Posts 利用 Google Guava 提供的便捷方法对 List 进行内存分页
Post
Cancel

利用 Google Guava 提供的便捷方法对 List 进行内存分页

有时候在某些特定场景下面我们经常需要对内存中的队列进行分页批量操作,但很多时候都是重复造轮子,很浪费时间;在此问题的前提下为了节约后续的时间与加大产出,发现了 Google Guava 的一系列工具包包括了此功能,所以在这里我记录下来后续方便查阅。

由于涉及到的内容比较单一,很多废话不多说了,直接参考下面的开始步骤。

开始

依赖

1
2
3
4
5
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

给定一组数据

1
List<Integer> numbers = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);

分页

主要利用以下方式切分List,返回一个二维集合(Matrix),由于我目前分2页,所以下面指定了每页为5条,参照以上给出的数据。

1
Iterables.partition(numbers,5)

完整代码

1
2
3
4
5
6
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

This post is licensed under CC BY 4.0

自动化测试入门教程(Maven + TestNG + Selenium)

黑群晖(XPEnology)无法启动&重建系统并保留数据经验总结

Comments powered by Disqus.