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()));
原文地址:https://www.cnblogs.com/julian-chang/p/11896996.html