May Java group&order&top in a call chain?

大兔子大兔子 提交于 2019-12-04 12:22:28

What you can do is using groupingBy with a downstream collector.

In your case maxBy will do the job for you. This will give you a Map<String, Optional<A>> where each key is mapped to an optional greatest value according to the comparator you supply.

Then you get the values of the map, filter them so that you only get non-empty optionals (avoiding a NSEE when calling get() on an Optional). You finally extract their content that you collect into a List.

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.toList;

...

List<A> resultList =
        list.stream()
            .collect(groupingBy(A::getGroupName, 
                                maxBy(comparingInt(A::getValue))))
            .values()
            .stream()
            .filter(Optional::isPresent)
            .map(Optional::get)
            .collect(toList());

Given your example, it outputs:

[A(1, A, 3), A(3, B, 7), A(4, C, 7)]

you can make it in one chain, but i think you will not get rid of the grouping.

list.stream()
            .collect(Collectors.groupingBy(a -> a.getGroupName(), Collectors.maxBy((A a1, A a2) -> a1.value - a2.value))).entrySet().stream()
            .map(v -> v.getValue().get()).collect(Collectors.toList());

As an alternative you can do it creating only one stream:

Collector<A, ?, Map<String, A>> groupingBy = groupingBy(
        A::getGroupName,
        collectingAndThen(maxBy(comparingInt(A::getValue)),
                Optional::get));
Collection<A> resultList = list.stream().collect(collectingAndThen(
        groupingBy, Map::values));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!