How to paginate a list of objects in Java 8?

后端 未结 3 1391
离开以前
离开以前 2020-12-05 05:50

Given a java.util.List with n elements and a desired page size m, I want to transform it to a map containing n/m+n%m elem

3条回答
  •  醉梦人生
    2020-12-05 06:14

    Simple solution using Guava: com.google.common.collect.Lists#partition:

        List> partition = Lists.partition(list, 3); //<- here
        Map map = IntStream.range(0, partition.size()).boxed().collect(Collectors.toMap(
                        Function.identity(),
                        i -> partition.get(i)));
    

提交回复
热议问题