JDK8 Lambda

之前看过JAVA 8的书,但由于时间长,平时没用所以忘的差不多了,大概记得格式。

常用代码

1.获得对象集合的某个属性倒序(最新)的一个对象

Optional<AppLog> appPLog = appLogList.stream().sorted(Comparator.comparing(AppLog::getCreationTime).reversed()).findFirst();

2.过滤条件的对象集合

List<User> filterList = list.stream().filter(user -> Integer.valueOf(user.getAge()) > 60).collect(Collectors.toList());

3.fastjson 数组去重

JSONArray.parseArray(JSON.toJSONString(array.stream().distinct().collect(Collectors.toList())))

4.按某一字段分组统计 

Map<String, DoubleSummaryStatistics> staffTimeResultMap = staffTimeList.stream().collect(Collectors.groupingBy(ProjectInfo::getName, Collectors.summarizingDouble(ProjectInfo::getTimes)));

5.按日期分组计数

Map<String, Long> countingMap = popupClickList.stream().collect(Collectors.groupingBy(lc -> sdf.format(lc.getLog_time()), Collectors.counting()));

 

posted @ 2019-11-20 12:50  julian_chang  阅读(209)  评论(0编辑  收藏  举报