List与Map互转
- List 转换为 Map
Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1,k2)->k1));
- Map转换为List
userMap.values();
list分组取最大项
Map<String, Optional<Record>> map=
maxRecords.stream().collect(
Collectors.groupingBy(Record::getDeviceId,
Collectors.maxBy(Comparator.comparing(Record::getMainLoad))));
map.values().stream().forEach(toGetMaxRecord->{
if(null!=toGetMaxRecord.get()){
Record item=toGetMaxRecord.get();
System.out.println(item.getMainLoad());
}
});
list排序
userList.sort(Comparator.comparing(User::getId));
userList.sort(Comparator.comparing(User::getId).thenComparing(User::getAge));
userList.sort(Comparator.comparing(User::getId).reversed());