Map<Long, List<Long>> resultMap = list.stream().collect(Collectors.groupingBy(UserRole::getUserId, Collectors.mapping(UserRole::getRoleId, Collectors.toList())));
Set<Serializable> dealHiddenDangerTypesSet = orgList.stream().map(Org::getDealHiddenDangerTypes)
.flatMap(List::stream).map(m -> (Serializable) m).collect(Collectors.toSet());
Integer[] integers = new Integer[] { 1, 2, 1, 3, 4, 4 };
List<Integer> distinctSort = Arrays.stream(integers).distinct().collect(Collectors.toList());
System.out.println(distinctSort);
- 第一种
private static List<UserCar> removeDuplicates1(List<UserCar> cars) {
return cars.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<UserCar>(Comparator.comparing(UserCar::getCarNo))), ArrayList::new)
);
}
- 第二种,hutool实现
private static List<UserCar> removeDuplicates2(List<UserCar> cars) {
return CollUtil.distinct(cars, UserCar::getCarNo, true);
}