Remove duplicates from ArrayLists

后端 未结 14 2184
孤街浪徒
孤街浪徒 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 05:02

    List list = (...);
    
    //list may contain duplicates.
    
    //remove duplicates if any
    Set setItems = new LinkedHashSet(list);
    list.clear();
    list.addAll(setItems);
    

    You may need to override "equals()" so that 2 elements are considered equals if they have the same subtitle (or tite and subtitle maybe ?)

提交回复
热议问题