Remove duplicates from ArrayLists

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

    private static List removeDuplicates(List list) {
        ArrayList uniqueList = new ArrayList();
        for (Integer i : list) {
            if (!inArray(i, uniqueList)) {
                uniqueList.add(i);
            }
        }
    
        return uniqueList;
    }
    
    private static boolean inArray(Integer i, List list) {
        for (Integer integer : list) {
            if (integer == i) {
                return true;
            }
        }
    
        return false;
    }
    

提交回复
热议问题