Java 8 Distinct by property

后端 未结 29 1831
傲寒
傲寒 2020-11-21 22:35

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

29条回答
  •  -上瘾入骨i
    2020-11-21 22:56

    Another library that supports this is jOOλ, and its Seq.distinct(Function) method:

    Seq.seq(persons).distinct(Person::getName).toList();
    

    Under the hood, it does practically the same thing as the accepted answer, though.

提交回复
热议问题