how to remove duplicate contacts from arraylist

前端 未结 5 1561
谎友^
谎友^ 2021-01-16 21:13

I have created an app in which I am getting the contacts from a device. But I want to remove the duplicate contacts from the results.

How could I do it?

5条回答
  •  时光取名叫无心
    2021-01-16 21:42

    use below code list=removeDuplicates(list);

    public List removeDuplicates(List list) {
        // Set set1 = new LinkedHashSet(list);
        Set set = new TreeSet(new Comparator() {
    
            @Override
            public int compare(Object o1, Object o2) {
                if (((ProfileBean) o1).getName().equalsIgnoreCase(((ProfileBean) o2).getName()) &&
                        ((ProfileBean)o1).getPhoneNumber().equalsIgnoreCase(((ProfileBean)o2).getPhoneNumber())) {
                    return 0;
                }
                return 1;
            }
        });
        set.addAll(list);
    
        final List newList = new ArrayList(set);
        return newList;
    }
    

提交回复
热议问题