Java 8 Distinct by property

后端 未结 29 1862
傲寒
傲寒 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:05

    My solution in this listing:

    List result ....
    
    List dto3s = new ArrayList<>(result.stream().collect(toMap(
                HolderEntry::getId,
                holder -> holder,  //or Function.identity() if you want
                (holder1, holder2) -> holder1 
        )).values());
    

    In my situation i want to find distinct values and put their in List.

提交回复
热议问题