groupingBy得到数值的Map
orderExecRateStatVOList.stream()
.collect(Collectors.groupingBy(OrderExecRateStatVO::getWardCode, Collectors.reducing(0, obj -> 1, Integer::sum)))
根据某些字段去重
orderList = orderList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(comparing(PatInhosOrder::getInhosCode))), ArrayList::new));
grouping之后根据Map的value的size排序
// 使用了ConcurrentHashMap
ConcurrentHashMap<String, List<DataRecords>> dataMap = records.stream().collect(Collectors.groupingBy(DataRecords::getCode, ConcurrentHashMap::new, Collectors.toList()));
//dataMap 根据记录数排序
// 将ConcurrentHashMap的内容转移到List中
List<Map.Entry<String, List<DataRecords>>> list = new ArrayList<>(dataMap.entrySet());
// 根据value的数量进行倒序排序
Collections.sort(list, new Comparator<Map.Entry<String, List<DataRecords>>>() {
@Override
public int compare(Map.Entry<String, List<DataRecords>> o1, Map.Entry<String, List<DataRecords>> o2) {
return Integer.compare(o2.getValue().size(), o1.getValue().size());
}
});
二维map
Map<String, Map<String, QmTraineeWardRotationDetailUser>> userMap = userList.stream()
.collect(Collectors.groupingBy(
QmTraineeWardRotationDetailUser::getPlanId, // 第一个键是 planId
Collectors.toMap(
QmTraineeWardRotationDetailUser::getUserCode, user -> user, (k1, k2) -> k1
)
));