Let Java group & order & top in call chain?

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 the id is unique, but the groupName is not. Then I have list 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 to filter the list by groupName and value, return the largest value eachName.

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


I knew I could write like

Map<String, List<A>> map =  list.stream().collect(

List<A> result = new ArrayList<A>();
  (s, a) -> {
          (o1, o2) -> o2.value.compareTo(o1.value)


And the question is, can I remove the middle card and they work in one chained call Like




source to share

3 answers

What you can do is use groupingBy

with a follow-up collector.

In your case, maxBy

will do the job for you. This will give you Map<String, Optional<A>>

where each key is mapped to the complementary highest value according to the comparator you supplied.

Then you get the map values, filter them so that you only get non-empty options (avoiding using NSEE when calling get()

on Optional

). You finally retrieve the content you are collecting into 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 =


Given your example, it outputs:

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




you can do it in one chain, but i guess you won't get rid of the grouping.

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




Alternatively, you can do this by creating just one thread:

Collector<A, ?, Map<String, A>> groupingBy = groupingBy(
Collection<A> resultList = list.stream().collect(collectingAndThen(
        groupingBy, Map::values));




All Articles