使用Guava Collections2提供的transform批量转换
- 工作小总结
- 时间:2018-08-24 16:36
- 2862人已阅读
简介
使用案例一: 实际开发了,为了快速查询,我们会把日期以Long类型的方式存储到数据库中,比如20000000000000L,但显示的时候,要完整的日期,即yyyy-MM-dd的格式显示。这个时候,我们就可以使用Collections2.transform方法处理类似的情况。HashSet<Long>set=Sets.newHashSet();  
🔔🔔🔔好消息!好消息!🔔🔔🔔
有需要的朋友👉:联系凯哥
使用案例一:
实际开发了,为了快速查询,我们会把日期以Long类型的方式存储到数据库中,比如20000000000000L,但显示的时候,要完整的日期,即yyyy-MM-dd的格式显示。
这个时候,我们就可以使用Collections2.transform方法处理类似的情况。
HashSet<Long> set = Sets.newHashSet();
set.add(20000000000000L);
set.add(30000000000000L);
set.add(40000000000000L);
Collection<String> result2 = Collections2.transform(set, input -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(input).toString());
result2.forEach(System.out::println);
输出
2603-10-11 19:33:20
3237-07-20 07:06:40
2920-08-30 13:20:00
使用案例二:
比如我们想从list中获取对象id:
testCollectionUtils() {
List<WqContractCourse> courseList = Lists.();
WqContractCourse c1 = WqContractCourse();
c1.setId();
WqContractCourse c2 = WqContractCourse();
c2.setId();
WqContractCourse c3 = WqContractCourse();
c3.setId();
WqContractCourse c4 = WqContractCourse();
c4.setId();
courseList.addAll(Lists.<WqContractCourse>(c1, c2, c3, c4));
Collection<Integer> courseIds = Collections2.(courseList, Function<WqContractCourse, Integer>() {
Integer apply(WqContractCourse wqContractCourse) {
wqContractCourse.getId();
}
});
.info(, JSONObject.(courseIds));
List<Integer> courseIds2 = Lists.();
Collection<WqContractCourse> deleteCourseList = Collections2.(courseList, Predicate<WqContractCourse>() {
apply(WqContractCourse wqContractCourse) {
(!.contains(wqContractCourse.getId())) {
.info(+ wqContractCourse.getId());
;
}
;
}
});
.info(, LogUtil.(Lists.(deleteCourseList)));
}上一篇: 项目复盘-模型
下一篇: idea 凯哥常用快捷键