One of my favorite of all the Collections classes of Java is the HashSet. Its easy to use and easy to understand. HashSet is actually a hash table implementation of the Map interface. The HashMap class is doesn’t accept multiple values. All its elements are unique.  If you attempt to add an existing element() using the add method, it will not be successful and the add() method will return false. Moreover, HashSet can contain null value.

In dealing with HashSet, a lot of beginners in java programming make a mistake on the ordering of the elements. The HashSet class is not ordered and not sorted at all. It doesn’t guarantee that first element that you add using add() method will also come in the same order. This is quite confusing because, I know that you are already familiar in using Arrays and List. Think of it as big bucket and you put things inside of it and when you retrieve the contents you wouldn’t know what you can get. With this concept in place, there are no method to access each individual element using indexes. Normally if you are dealing with arrays we tend to get specific element through an index like for example value[0] and also if you are using list that would be value.get(0). In HashSet there are no method similar to those, rather the only way to access all the elements is through the use of iterator() method.

Now that we have described the HashSet let’s take a look on a simple example.

Basic Example of HashSet

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

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

/*
 * A basic example in dealing with HashSet
 */

public class HashSetBasicExample {

	public static void main(String[] args) throws InterruptedException {

		// declare the HashSet object
		HashSet studentSet = new HashSet();
		// populate the HashSet
		studentSet.add("Shyra Travis");
		studentSet.add("Sharon Wallace");
		studentSet.add("Leo Batista");

		// display the contents of our set
		Iterator it = studentSet.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}

	}	

}

The above example will give on the following result

Sharon Wallace
Leo Batista
Shyra Travis

The following fundamental concepts has been evaluated on the said example:

  • How to instantiate a HashSet. Specifying the object type of the elements
  • Its also been shown how to add elements on the HashSet using the add method.
  • The itereator() method were used to display all the contents of HashSet.

HashSet Method Usage Examples

The following are the detailed list of HashSet methods and descriptions. We have also provided links to examples of each method on the list.

Modifier and Type Method and Description
boolean add(E e)
Basically this method provide a facility to add elements to the HashSet object. A boolean returned value will indicate if the addition of element succeeded.It will return true if it is, otherwise false. If the HashSet already contains the same as what we are trying to add, a return value would be false.
void clear()
this method provide a facility to remove all the contents of the HashSet.
Object clone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
boolean contains(Object o)
The contains method check the HashSet object if it contains the specified method argument Object. A boolean returned value will indicate if the set is contains the parameter Object or not.It will return true if it is, otherwise false.
boolean isEmpty()
This method check if the set has elements or it is empty. A boolean returned value will indicate if the set is empty or not, true if it is otherwise false.
Iterator iterator()
This method returns an iterator to all the elements of the HashSet.
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).
Spliterator spliterator()
Creates a late-binding and fail-fast Spliterator over the elements in this set.