如何在java中找到arraylist的重复对象

时间:2014-02-14 15:15:04

标签: java collections arraylist

您好我想找到一个类类型的数组列表的重复对象。我尝试使用哈希集但不起作用。有人可以帮忙吗。

package stream;

import java.util.*;

public class Chumma {

public static void main(String[] args) {
    Dummy d = new Dummy();
    Dummy d1 = new Dummy();
    Dummy d2 = new Dummy();
    d.setAge(14);
    d.setName("XXX");
    d1.setAge(15);
    d1.setName("YYY");
    d2.setAge(14);
    d2.setName("XXX");
    List<Dummy> list = new ArrayList<Dummy>();
    list.add(d);
    list.add(d1);
    list.add(d2);
    Set<Dummy> uniqueSet = new HashSet<Dummy>(list);
    Set uniqueEntries = new HashSet<Dummy>();
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {
        Object element = iter.next();
        if (!uniqueEntries.add(element)) // if current element is a duplicate,
            // iter.remove();
            System.out.println(iter.toString());
        }
    }
}

3 个答案:

答案 0 :(得分:5)

您可以使用

Collections.frequency(collection, object);

因此,如果frequency方法返回一个数字> 1,则意味着您有更多相同的对象......

Collectionsjava.util.Collections

获取

和metod 返回指定集合中的元素数量等于指定的

在这里你是api:

frequency(c, o)

答案 1 :(得分:0)

java.lang.Object提供的equals()方法的默认实现比较内存位置,只有两个引用变量指向同一内存位置时才返回true。 equals()方法用于避免在HashSet上重复。

问题是你没有覆盖equals()和hash()方法。

  

常见的错误来源是无法覆盖hashCode方法。您必须覆盖覆盖等于的每个类中的hashCode。如果不这样做,将导致违反Object.hashCode的一般合同,这将阻止您的类与所有基于散列的集合(包括HashMap,HashSet和Hashtable)一起正常运行。 [有效的Java]

import java.util.*;

public class HashSetTest {

    public static class Dummy {
        private int age;
        private String name;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Dummy)) return false;

            Dummy dummy = (Dummy) o;

            if (age != dummy.age) return false;
            if (name != null ? !name.equals(dummy.name) : dummy.name != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = age;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }

        @Override
        public String toString() {
            return String.format("age:%d, name:%s", age, name);
        }
    }

    public static void main(String[] args) {
        Dummy dummy01 = new Dummy();
        dummy01.setAge(14);
        dummy01.setName("XXXX");

        Dummy dummy02 = new Dummy();
        dummy02.setAge(15);
        dummy02.setName("YYYY");

        Dummy dummy03 = new Dummy();
        dummy03.setAge(14);
        dummy03.setName("XXXX");

        List<Dummy> dummies = new ArrayList<Dummy>();
        dummies.add(dummy01);
        dummies.add(dummy02);
        dummies.add(dummy03);

        Set<Dummy> uniqueDummies = new HashSet<Dummy>();
        for (Dummy dummy : dummies) {
            uniqueDummies.add(dummy);
        }

        System.out.println(uniqueDummies);
    }
}

答案 2 :(得分:0)

您可以访问此Link

此链接的示例代码

//ArrayList with duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
System.out.println("size of Arraylist with duplicates: " + duplicateList.size()); //should print 4 becaues of duplicates Android

System.out.println(duplicateList);

//Converting ArrayList to HashSet to remove duplicates
HashSet<String> listToSet = new HashSet<String>(duplicateList);

//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
System.out.println("size of ArrayList without duplicates: " + listToSet.size()); //should print 3 becaues of duplicates Android removed

System.out.println(listWithoutDuplicates);

Output:
size of Arraylist with duplicates: 4
[Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
[Android, Windows mobile, iOS]