Remove duplicates from ArrayLists

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

    Removes any duplicates in a collection, while preserving the order if it is an ordered collection. Efficient enough for most cases.

    public static > T removeDuplicates(T collection)
    {
        Set setItems = new LinkedHashSet(collection);
        collection.clear();
        collection.addAll(setItems);
    
        return collection;
    }
    

提交回复
热议问题