Java 8 Distinct by property

后端 未结 29 1871
傲寒
傲寒 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 22:59

    The Most simple code you can write:

        persons.stream().map(x-> x.getName()).distinct().collect(Collectors.toList());
    

提交回复
热议问题