版权声明:无私奉献,希望大家转载注明出处,谢谢! https://blog.csdn.net/littlebrain4solving/article/details/78845538
说明
有时候在某些特定场景下面我们经常需要对内存中的队列进行分页批量操作,但很多时候都是重复造轮子,很浪费时间;在此问题的前提下为了节约后续的时间与加大产出,发现了 Google Guava
的一系列工具包包括了此功能,所以在这里我记录下来后续方便查阅。
由于涉及到的内容比较单一,很多废话不多说了,直接参考下面的开始步骤。
开始
依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
给定一组数据
List<Integer> numbers = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);
分页
主要利用以下方式切分List,返回一个二维集合(Matrix),由于我目前分2
页,所以下面指定了每页为5
条,参照以上给出的数据。
Iterables.partition(numbers,5)
完整代码
List<Integer> numbers = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);
Iterables.partition(numbers,5).forEach(i -> {
System.out.println("--- seperator ---");
i.forEach(System.out::println);
});
System.out.println("--- The end ---");
最后
更多 Guava 的用法参考其代码库 Wiki 文档: https://github.com/google/guava/wiki