Remove duplicates from ArrayLists

后端 未结 14 2183
孤街浪徒
孤街浪徒 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:46

    Update for Java8:

    Using Java8 streams you can also do pretty trivally.

    ArrayList deduped;
    deduped = yourArrayList.stream()
                 .distinct()
                 .collect(Collectors.toCollection(ArrayList::new));
    

    This also has the advantage over going ArrayListSetArrayList of maintaining ordering.

提交回复
热议问题