public static void main(String[] args) throws IllegalAccessException {
// public Name(String nameCode, String cay, int sum, int count, int doub) {
List<Name> excelDtoList = new ArrayList<>();
Name testCar1 = new Name("431300000000","娄星区",1,2,3);
Name testCar3 = new Name("431010000000","雨花区",1,2,3);
Name testCar2 = new Name("431010000000","天心区",2,2,3);
excelDtoList.add(testCar1);
excelDtoList.add(testCar3);
excelDtoList.add(testCar2);
System.out.println("-------------list分组--------------");
Map<String,List<Name>>map = excelDtoList.stream().collect(Collectors.groupingBy(Name::getNameCode));
map.forEach((s, names) -> {
System.out.println(s+" :");
names.stream().forEach(item->{
System.out.println(item.toString());
});
});
System.out.println("-------------list分组求和--------------");
Map<String,Integer> mapSum = excelDtoList.stream()
.collect(Collectors.groupingBy(Name::getNameCode,Collectors.summingInt(Name::getSum)));
mapSum.forEach((s, integer) -> {
System.out.print(s+":");
System.out.println(integer);
});
System.out.println("-------------list去重--------------");
//对象去重
List<Name> distinctList = excelDtoList.stream().filter(distinctByKey(Name::getNameCode)).collect(Collectors.toList());
distinctList.forEach(item->{
System.out.println(item);
});
}
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

image