Remove duplicates from ArrayLists

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

    I would suggest using a Set

    http://download.oracle.com/javase/6/docs/api/java/util/Set.html

    Which by its nature cannot contain duplicate items. You can create a new set from your original ArrayList using

    Set myset = new HashSet(myArrayList);
    

    Alternatively, just use a Set from the start, and don't use an ArrayList as it is not performing the function that you require.

提交回复
热议问题