Remove duplicates from ArrayLists

后端 未结 14 2223
孤街浪徒
孤街浪徒 2020-11-27 04:12

I have an ArrayList of custom objects. I want to remove duplicate entries.

The objects have three fields: title, subtitle, and id. If a su

14条回答
  •  囚心锁ツ
    2020-11-27 04:42

    Another method using Java 8 streams you can also do pretty cool:

    List CustomerLists;
    List unique = CustomerLists.stream().collect(collectingAndThen(
            toCollection(() -> new TreeSet<>(comparingLong(Customer::getId))),
            ArrayList::new));
    

提交回复
热议问题