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

会有一股神秘感。 提交于 2019-12-06 08:15:32

问题


I have a POJO class

class A {
  public int id;
  public String groupName;
  public String getGroupName() { return this.groupName; }

  public int value;

  public A(int id, String groupName, int value) {
    this.id = id;
    this.groupName = groupName;
    this.value = value;
  }
}

And id is Unique, but groupName is not. Then I have a List of A.

List<A> list = new ArrayList<A>();
list.add(new A(1, "A", 3));
list.add(new A(2, "B", 5));
list.add(new A(3, "B", 7));
list.add(new A(4, "C", 7));

I want filter the list by groupName and value, return the biggest value each groupName.

List<B> filtedList = list....
//filtedList contain
//A(1, 'A', 3) A(3, 'B', 7) A(4, 'C', 7)

I knew that I can code like this

Map<String, List<A>> map =  list.stream().collect(
    Collectors.groupingBy(A::getGroupName)
);

List<A> result = new ArrayList<A>();
map.forEach(
  (s, a) -> {
      result.addAll(
        deliveryOrderItems.stream().sorted(
          (o1, o2) -> o2.value.compareTo(o1.value)
        ).limit(1).collect(Collectors.toList())
      );
  }
);

And the question is, Can I remove the middle Map and do those operate in one chain call Like

//list.stream().groupBy(A::getGroupName).orderInGroup(A::value).topInGroup(1)

回答1:


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)]



回答2:


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());



回答3:


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));


来源:https://stackoverflow.com/questions/31604284/may-java-groupordertop-in-a-call-chain

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