java8关于stream的部分用法
list集合分组后按时间排序取最近的一条记录
集合按照id进行分组后,按照时间顺序取最近一条记录
List<Documnet> list = new ArrayList();
Map<String, Document> map = list.parallelStream().collect(Collectors.groupingBy(data -> data.getString("id"),
Collectors.collectingAndThen(Collectors.reducing(
(c1, c2) -> DateUtil.compare(c1.getDate("data_time"), c2.getDate("data_time")) > 0 ? c1 : c2),
Optional<Document>::get
)));
list排序
List<User> list = new ArrayList();
// 正序
List<User> collect = list.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
//倒序
List<User> collect = list.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
//常用数据类型排序
List<Date> dateList = new ArrayList();
List<Date> collect =dateList.stream().sorted().collect(Collectors.toList());
List<Date> collect =dateList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());