Unwrap Optional when using Collectors maxBy with groupingBy

我的梦境 提交于 2020-08-25 09:32:52

问题


I have a class with a String and an int field.

public class Data {
    private String name;
    private int value;

    private Data(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }
}

I have a List<Data> and if I want to create a map (grouping) to know the Data object with maximum value for each name, I could do like,

Map<String, Optional<Data>> result = list.stream()
            .collect(Collectors.groupingBy(Data::getName,
                    Collectors.maxBy(Comparator.comparing(Data::getValue))));

But, the RHS is wrapped in an Optional because of the contract of Collectors.maxBy but I want a Map<String, Data>.

One option I could think of it to use Collectors.collectingAndThen, but I will have to create an unnecessary intermediate map.

list.stream()
         .collect(Collectors.collectingAndThen(Collectors.groupingBy(Data::getName,
                Collectors.maxBy(Comparator.comparing(Data::getValue))),
                map -> map.entrySet().stream()
                        .collect(Collectors.toMap(Map.Entry::getKey,
                                 e -> e.getValue().get()))));

Is there an idiomatic way to achieve this using streams?


回答1:


You can use BinaryOperator.maxBy with Collectors.toMap

Map<String, Data> result = list.stream()
        .collect(Collectors.toMap(Data::getName, Function.identity(),
            BinaryOperator.maxBy(Comparator.comparing(Data::getValue))));


来源:https://stackoverflow.com/questions/63159338/unwrap-optional-when-using-collectors-maxby-with-groupingby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!