Understanding HashSet in java

HashSet

 
HashSet class implements the Set interface.
 
HashSet provides no guarantee about the order in which elements be retrieved.
 
It allows storing only one NULL element. All subsequent calls to store NULL values are ignored.
 
 

HashSet and Synchronization

 
HashSet is not synchronized.
 
A synchronized wrapper around the HashSet can be obtained using :
 
Set s = Collections.synchronizedSet(new HashSet(…));

 
 

HashSet Constructors

 
HashSet is implemented using HashMap.
 
Most of the code that provides the functionality of a HashSet is actually defined in HashMap and its related classes.
 

HashSet()

 
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
 

HashSet(int initialCapacity)

 
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
 

HashSet(int initialCapacity, float loadFactor)

 
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
 

HashSet(Collection c)

 
Constructs a new set containing the elements in the specified collection.
 
 

HashSet elements and buckets

 
HashSet uses hashing algorithms to store, remove, and retrieve its elements.
 
When an object is added to the Set, its hash code is used to choose a “bucket” into which to place the object.
 
When we check if a Set contains a particular object, the object’s hashcode is used to determine the bucket location.
 
Then it iterate’s through the objects in that bucket and tries to find the object using equals() method.
 
So, the hashcode method find the bucket and the equals method determines whether the bucket contains the object.
 
 

HashSet and Duplicates

 
In a HashSet, duplicate elements are not allowed. Here duplicate means two elements have same hashcode and return true when compared with equals() method.
 
Here is an example for this :
 

package com.topjavatutorial;

import java.util.HashSet;

public class HashSetDemo1 {

  public static void main(String[] args) {

    String s1= new String("JAVA");
    String s2 = new String("SCALA");
    String s3 = new String("JAVA");
    
    HashSet<String> set = new HashSet<String>();
    
    set.add(s1);
    set.add(s2);
    set.add(s3);//Duplicate.. will not be added to set
    
    for(String tech:set)
      System.out.println(tech);
    
    
  }

}


 
In above example, string s3 contains same value as string s1.
 
Since, String class overrides equals to check for equality, these two string will be found equal and s3 won’t be added to the set.
 
 
However, if you try the same example using custom class that does not override hashcode and equals methods properly, you will be getting unexpected result.
 
For example, if we use an Employee class that doesn’t override equals method, the duplicate elements will be added.
 
package com.topjavatutorial;

import java.util.HashSet;

public class HashSetDemo1 {

  public static void main(String[] args) {

    Employee e1 = new Employee(1,"A");
    Employee e2 = new Employee(1,"B");
    Employee e3 = new Employee(1,"A");
    
    HashSet<Employee> set = new HashSet<Employee>();
    
    set.add(e1); // Employee A added
    set.add(e2); // Employee B added
    set.add(e3); // Employee A added .. not considered a duplicate as Employee doesn't override equals()
    
    System.out.println(set.size());
    //prints 3
  }

}


 
The above example will print 3.

 
 

HashSet methods

 
Here are some of the frequently used HashSet methods for reference :
 

boolean add(E e)

Adds the specified element to this set if it is not already present.
 

void clear()

Removes all of the elements from this set.
 

boolean contains(Object o)

Returns true if this set contains the specified element.
 

boolean isEmpty()

Returns true if this set contains no elements.
 

Iterator iterator()

Returns an iterator over the elements in this set.
 

boolean remove(Object o)

Removes the specified element from this set if it is present.
 

int size()

Returns the number of elements in this set (its cardinality).

 
 

You may also like following articles on Java Collections:

 

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags