Guava Lists工具类
工具 Guava lists
2023-09-11 14:15:40 时间
01 概述
Guava
是 Google
开源的一个 Java
工具库,里面有很多工具类,本文要讲的是里面的Lists
工具类。
注意,使用Guava
工具类库,必须先添加依赖:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0</version>
</dependency>
02 Lists工具类
从下图可以看到Lists
工具类有很多种方法:
下面举几个比较典型的操作演示下:
① 使用一行代码就能创建集合了,如下:
ArrayList<Student> students = Lists.newArrayList(
new Student("张三", 33),
new Student("李四", 40),
new Student("王五", 23),
new Student("赵六", 55));
② 集合间转换(Sets
也是仿照Lists
的),如下:
HashSet<String> strHashSet = Sets.newHashSet("1", "2", "3");
ArrayList<String> strList = Lists.newArrayList(strHashSet);
③ 集合分页(虽然stream流也能做到,也看下吧),如下:
ArrayList<Student> students = Lists.newArrayList(
new Student("张三", 33),
new Student("李四", 40),
new Student("王五", 23),
new Student("赵六", 55));
// 每页两条
List<List<Student>> partionStudent = Lists.partition(students, 2);
④ dto转vo时用到,如下:
List<StudentVo> studentVoList = Lists.transform(studentList, new Function<Student, StudentVo>() {
@Override
public StudentVo apply(Student student) {
StudentVo s = new StudentVo();
try {
BeanUtils.copyProperties(s, student);
} catch (Exception e) {
}
s.setStudent_id(student.getId());
s.setStudent_no(student.getStuNo());
return s;
}
});
03 文末
本文就举Lists
用法的几个典型场景例子,不过这工具类也是有不完善之处的(例如:transform方法),各位童鞋按需使用吧。
相关文章
- 网络抓包工具 Fiddler
- 数仓工具—Hive实战之UDF 加密(20)
- Base64工具类
- 在 Linux 中使用"avconv"工具录制计算机桌面视频和音频
- 程序性能调优工具之gprob
- 《HTML5开发手册》——1.10 中级“菜谱”:使用HTML5大纲工具验证结构
- 自定义分页工具类(仿百度搜索分页方式)
- Java两大工具库:Commons和Guava
- Unity 工具类 之 简单网络下载任务队列服务实现(WWW/UnityWebRequest/下载AssetBundle/Audioclip/Texture2D/Text等)
- 在智慧城市建设中 计算机模拟是一个强大的工具
- Google联手Jigsaw发布免费的DDoS防御工具:防止黑客再次破坏选举
- 多平台密码绕过及提权工具Kon-Boot的使用与防范
- vue 调试工具 vue-devtools