In Java 8 how can I filter a collection using the Stream
API by checking the distinctness of a property of each object?
For example I have a list of
Maybe will be useful for somebody. I had a little bit another requirement. Having list of objects A
from 3rd party remove all which have same A.b
field for same A.id
(multiple A
object with same A.id
in list). Stream partition answer by Tagir Valeev inspired me to use custom Collector
which returns Map
. Simple flatMap
will do the rest.
public static Collector>> groupingDistinctBy(Function keyFunction, Function distinctFunction) {
return groupingBy(keyFunction, Collector.of((Supplier