Using Set - Removes duplicates automatically?

13,937

Solution 1

Now if I keep adding the object to the set, does it automatically remove duplicates based on the values of the each elements.

The element if found duplicate by the Set implementation ,wouldn't be inserted at all.

From the Javadocs of HashSet#add(E e):

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.

Solution 2

The short answer is yes, any set will perform the de-duplication by itself. But that's only guaranteed if you respect the contract; and the contract is a bit different for each Set implementation:

For a HashSet, as you noticed, you have to implement hashCode() and equals(). But that's not enough: if you keep mutable instances in a HashSet and you modify the properties which affect the hashCode()/equals() result, you'll still end up in a weird situation.

With a TreeSet, on the other hand, you need to make sure that your Comparable.compareTo() or Comparator.compare() methods are consistent with equals().

So read the documentation of Set and the one of the actual implementation that suits your needs and try to stick to the contract.

Solution 3

Yes, any Set implementation would not allow duplicates.

But, it comes with a caveat. For a Set and other hash-based collections you need to make sure that the objects that you try to insert override their equals() as well as hashCode() method correctly.

By correct, I mean that equals() should return true for two meaningfully equivalent objects and that the hashCode() should try to return as disparate values as possible for the range of objects in your input set.

While an improper equals() implementation would most likely break your program; an inefficient hashCode() implementation would degrade your Set's performance i.e. operations like add(), contains() etc. would be as slow as bad the implementation.

Solution 4

HashSet internally uses HashMap, this map is maintained with key as the value that you want to add in the set and value is an instance of an Object class( final private static final Object PRESENT = new Object();). Now, when you add same object to this set, its the same operation that map does when you do map.put with the same on the same key, just updating the value.

However the value to be updated still remains the same which is PRESENT. And hence in a way you can say that set does not add same value. Though actually, map which is used internally to implement this set is updating itself with *same key* and same value(PRESENT).

Share:
13,937
Kevin Rave
Author by

Kevin Rave

Updated on June 04, 2022

Comments

  • Kevin Rave
    Kevin Rave almost 2 years

    I have a class as below (Bean):

    class KeysHolder {
       Long kitId;
       String packId;
       String boxId;
    
       // getters and setters
        .......
       // @Override equals and hashCode
    }
    

    I have a set that contains the above Objects

     Set<KeysHolder> keys;
    

    Now if I keep adding the object to the set, does it automatically remove duplicates based on the values of the each elements? (I understand it uses Overridden equals and hashCode methods internally).

    The final set should not contain any duplicates based on each field equality.

      obja.kitId = objb.kitId
      objb.packid = objb.packId
      obja.boxId = objb.boxId
    

    Thanks!

  • Kevin Rave
    Kevin Rave almost 11 years
    Yes, thats what I was intending to say after all. :-) Thanks anyways
  • Kevin Rave
    Kevin Rave almost 11 years
    So it does not add duplicate elements based on the each element equality?
  • AllTooSir
    AllTooSir almost 11 years
    Correct at least the doc says so , internally it HashSet uses Hashmap#put().
  • zerocool
    zerocool almost 11 years
    KevinRave As @TheNewIdiot said, Hashset internally uses HashMap, just check post for better understanding