Java+多级+groupingby,java stream Collectors.groupingBy()多个字段

Stream> duplicates = notificationServiceOrderItemDto.getService()

.getServiceCharacteristics()

.stream()

.collect(

Collectors.groupingBy(

ServiceCharacteristicDto::getName, Collectors.counting()

)

)

.entrySet()

.stream()

.filter(e -> e.getValue() > 1);

Optional dupName = duplicates.map(Map.Entry::getKey).findFirst();

works perfect. But I wold like to find duplicates not just with name but also name + value + key

That means if name + value + key is the same this is duplicate.

I am looking Collectors.groupingBy()

but I can not find correct solution

解决方案

Instead of

.collect(Collectors.groupingBy(ServiceCharacteristicDto::getName, Collectors.counting()))

you can write

.collect(Collectors.groupingBy(s->s.getName()+'-'+s.getValue()+'-'+s.getKey(), Collectors.counting()))

你可能感兴趣的:(Java+多级+groupingby,java stream Collectors.groupingBy()多个字段)