Java HashSet Class

Category: Java   Tags: Java, Java Collection, Java Set Interface, Java HashSet Class

HashSet class extends AbstractSet and implements the Set interface. HashSet is a generic class that has this declaration:

                                    public class HashSet<E>
                                    extends AbstractSet<E>
                                    implements Set<E>, Cloneable, Serializable
                                

Here, E specifies the type of object that this set will contain. HashSet use a hash table for storage. A hash table stores information by using a mechanism called hashing.

What Is Hashing?

In hashing, a key is used to determine a unique value called hash code. The hash code is then used as the index at which the data associated with the key is stored. Using hashing keeps the add, remove, size operations to remain constant even for large sets.

Constructors

public HashSet()
Constructs a new, empty set.
public HashSet(int capacity)
It will create an empty HashSet with an initial capacity specified by capacity and default load factor (0.75).
HashSet(int capacity, float loadFactor)
It will create an empty HashSet with an initial capacity specified by capacity and load factor loadFactor.
HashSet(Collection<? extends E> c)
It creates a HashSet containing the elements of the specified collection c.

It is very important not to set the initial capacity too high or the load factor too low; if iteration performance is important.

HashSet is not synchronized. Therefore if multiple thread is accessing the set then it must synchronized externally. You can check out this post about synchronization. You can use Collections.synchronizedSet also to synchronize the HashSet:

                                    Set set = Collections.synchronizedSet(new HashSet());
                                

The returned iterator of this class are fail-fast i.e during iterating the HashSet if you do any modification, it will throw ConcurrentModificationException exception. Here is one HashSet example:

HashSetDemoExample.java

                            package com.tutorial.java.collections;

                            import java.util.HashSet;
                            import java.util.Iterator;

                            public class HashSetDemoExample {

                                public static void main(String[] args) {
                                    HashSet<Integer> set = new HashSet<>();
                                    set.add(13);
                                    set.add(10);
                                    set.add(11);
                                    System.out.println("Size: " + set.size());

                                    //Iterate the HashSet
                                    System.out.println("HashSet iteration: ");
                                    Iterator<Integer> iterator = set.iterator();
                                    while (iterator.hasNext()) {
                                        Integer element = iterator.next();
                                        System.out.println(element);
                                    }
                                }

                            }
                        

Output:

                            Size: 3
                            HashSet iteration:
                            10
                            11
                            13
                          

As you can see that HashSet keeps the data in sorted form.