核心:
List<AreaAging> unique = newDetails.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getArriveProvince() + ";" + o.getHairCity()))), ArrayList::new)
);
示例:
AreaAging 实体
List<AreaAging> newDetails = Lists.newArrayList();
AreaAging temp1 = new AreaAging();
temp1.setArriveProvince("巴基斯坦");
temp1.setHairCity("卡拉奇");
newDetails.add(temp1);
AreaAging temp2 = new AreaAging();
temp2.setArriveProvince("巴基斯坦");
temp2.setHairCity("伊斯兰堡");
newDetails.add(temp2);
System.out.println(JSON.toJSONString(newDetails));
// 根据name,sex两个属性去重
// 根据name去重
List<AreaAging> unique = newDetails.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getArriveProvince() + ";" + o.getHairCity()))), ArrayList::new)
);
System.out.println(JSON.toJSONString(unique));
List通过stream流转Map
Map<String, String> map = list.stream().collect(HashMap::new, (m, e) -> m.put(e.getStartTime(), e.getName() + ":" + e.getStudents(), HashMap::putAll);