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)
you can make it in one chain, but i think you will not get rid of the grouping.
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 aMap<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 anOptional
). You finally extract their content that you collect into aList
.Given your example, it outputs:
As an alternative you can do it creating only one stream: