本文介绍了java stream Collectors.groupingBy()多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stream<Map.Entry<String, Long>> duplicates = notificationServiceOrderItemDto.getService()
    .getServiceCharacteristics()
    .stream()
    .collect(
        Collectors.groupingBy(
            ServiceCharacteristicDto::getName, Collectors.counting()
        )
     )
     .entrySet()
     .stream()
     .filter(e -> e.getValue() > 1);

Optional<String> 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

我正在寻找Collectors.groupingBy()

I am looking Collectors.groupingBy()

http://www.technicalkeeda.com/java -8-tutorials/java-8-stream-grouping

但我找不到正确的解决方案

but I can not find correct solution

推荐答案

代替

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

你可以写

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

这篇关于java stream Collectors.groupingBy()多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:34