Java 8 Distinct by property

后端 未结 29 1868
傲寒
傲寒 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条回答
  •  梦谈多话
    2020-11-21 23:00

    I recommend using Vavr, if you can. With this library you can do the following:

    io.vavr.collection.List.ofAll(persons)
                           .distinctBy(Person::getName)
                           .toJavaSet() // or any another Java 8 Collection
    

提交回复
热议问题