0

Hi I want to find a duplicate object of array list of one class type. I tried using hash set but is not working. Can somebody please help.

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());
        }
    }
}
7
  • 4
    Does you Dummy class implement .equals() and .hashCode()? Commented Feb 14, 2014 at 15:18
  • 1
    HINT : docs.oracle.com/javase/7/docs/api/java/util/… Commented Feb 14, 2014 at 15:19
  • Also, there are much more simple ways to do what you want using the collections API. Why don't you just .addAll(list) to the Set and use this instead of your current code? Also, HashSet does not respect insertion order upon iteration. Commented Feb 14, 2014 at 15:22
  • 1
    Start by defining "duplicate". Java can't know what you consider a duplicate if you don't tell him. Commented Feb 14, 2014 at 15:22
  • 1
    Well, you should implement what I said in the first comments. If you don't, your code will use Object's .equals() and .hashCode(), which is not what you want (basically, two Objects are equal only if they are the same reference; not what you want) Commented Feb 14, 2014 at 15:28

3 Answers 3

4

You can use

Collections.frequency(collection, object);

So if frequency method return a number > 1 it means that you have more same object...

Collections is get by java.util.Collections

and that metod Returns the number of elements in the specified collection equal to the specified

Here you are the api:

frequency(c, o)

Sign up to request clarification or add additional context in comments.

Comments

0

Default implementation of equals() method provided by java.lang.Object compares memory location and only return true if two reference variable are pointing to same memory location. equals() method used to avoid duplicates on HashSet.

The problem is that you didn't override the equals() and hash() methods.

A common source of bugs is the failure to override the hashCode method. You must override hashCode in every class that overrides equals. Failure to do so will result in a violation of the general contract for Object.hashCode, which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable. [Effective 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);
    }
}

Comments

0

You Can Visit this Link

Sample Code of this 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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.