The perfect place for easy learning...

Java Programming

×

Topics List


Java HashSet Class





The HashSet class is a part of java collection framework. It is available inside the java.util package. The HashSet class extends AbstractSet class and implements Set interface.

The elements of HashSet are organized using a mechanism called hashing. The HashSet is used to create hash table for storing set of elements.

The HashSet class is used to create a collection that uses a hash table for storing set of elements.

🔔 The HashSet is a child class of AbstractSet

🔔 The HashSet implements interfaces like Set, Cloneable, and Serializable.

🔔 The HashSet does not allows to store duplicate data values, but null values are allowed.

🔔 The HashSet does not maintains the order of insertion.

🔔 The HashSet initial capacity is 16 elements.

🔔 The HashSet is best suitable for search operations.

HashSet class declaration

The HashSet class has the following declaration.

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

HashSet class constructors

The HashSet class has the following constructors.

  • HashSet( ) - Creates an empty HashSet with the default initial capacity (16).
  • HashSet(Collection c) - Creates a HashSet with given collection of elements.
  • HashSet(int initialCapacity) - Creates an empty HashSet with the specified initial capacity.
  • HashSet(int initialCapacity, float loadFactor) - Creates an empty HashSet with the specified initial capacity and loadFactor.

Operations on HashSet

The HashSet class allow us to perform several operations like adding, accesing, deleting, updating, looping, etc. Let's look at each operation with examples.

Adding Items

The HashSet class has the following methods to add items.

  • boolean add(E element) - Inserts given element to the HashSet.
  • boolean addAll(Collection c) - Inserts given collection of elements to the HashSet.

Let's consider an example program to illustrate adding items to the HashSet.

Example
import java.util.*;

public class HashSetExample {

	public static void main(String[] args) {

		HashSet set = new HashSet();
		HashSet anotherSet = new HashSet();

		set.add(10);
		set.add(20);
		set.add(30);
		set.add(40);
		set.add(50);
		
		System.out.println("\nHashSet is\n" + set);
		
		anotherSet.addAll(set);

		System.out.println("\nanotherSet is\n" + anotherSet);
		
	}

}

HashSet class in java

Accessing Items

The HashSet class has no methods to access items, we can access whole set using its name.

Updating Items

The HashSet class has no methods to update or change items.

Removing Items

The HashSet class has the following methods to remove items.

  • boolean remove(Object o) - Removes the specified element from the invoking HashSet.
  • boolean removeAll(Collection c) - Removes all the elements of specified collection from the invoking HashSet.
  • boolean removeIf(Predicate p) - Removes all of the elements of HashSet collection that satisfy the given predicate.
  • boolean retainAll(Collection c) - Removes all of the elements of HashSet collection except specified collection of elements.
  • void clear( ) - Removes all the elements from the HashSet.

Let's consider an example program to illustrate removing items from the HashSet.

Example
import java.util.*;

public class HashSetExample {

	public static void main(String[] args) {

		HashSet set = new HashSet();
		HashSet anotherSet = new HashSet();

		set.add(10);
		set.add(20);
		set.add(30);
		set.add(40);
		set.add(50);
		
		System.out.println("\nHashSet is\n" + set);
		
		anotherSet.addAll(set);

		System.out.println("\nanotherSet is\n" + anotherSet);
		
		set.remove(20);
		System.out.println("\nHashSet after remove(20) is\n" + set);
		
		anotherSet.removeAll(set);
		System.out.println("\nanotherSet after removeAll(set) is\n" + anotherSet);
		
		set.retainAll(anotherSet);
		System.out.println("\nset after retainAll(anotherSet) is\n" + set);
		
		anotherSet.clear();
		System.out.println("\nanotherSet after clear() is\n" + anotherSet);
		
	}

}

HashSet class in java

Other utility methods

The HashSet class has the following methods to work with elements of it.

  • int size( ) - Returns the total number of elements in the invoking HashSet.
  • boolean isEmpty( ) - Returns true if the HashSet is empty otherwise returns false.
  • HashSet clone( ) - Returns a copy of the invoking HashSet.
  • boolean contains(Object element) - Returns true if the HashSet contains given element otherwise returns false.
  • boolean containsAll(Collection c) - Returns true if the HashSet contains given collection of elements otherwise returns false.
  • boolean equals(Object o) - Compares the specified object with invoking HashSet collection for equality.
  • int hashCode( ) - Returns the hash code of the invoking HashSet.
  • Object[ ] toArray( ) - Returns an array of Object instances that contains all the elements from invoking HashSet.
  • Spliterator spliterator( ) - Creates spliterator over the elements in a HashSet.
  • Iterator iterator( ) - Returns an iterator over the elements in the HashSet. The iterator does not return the elements in any particular order.

Let's consider an example program to illustrate other utility methods of the HashSet.

Example
import java.util.*;

public class HashSetExample {

	public static void main(String[] args) {

		HashSet set = new HashSet();

		set.add(10);
		set.add(20);
		set.add(30);
		set.add(40);
		set.add(50);
		
		System.out.println("\nHashSet is\n" + set);
		
		System.out.println("\nsize() - " + set.size());
		
		System.out.println("\nisEmpty() - " + set.isEmpty());
		
		System.out.println("\ncontains(30) - " + set.contains(30));
		
		System.out.println("\nequals(30) - " + set.equals(30));
		
		System.out.println("\nhashCode() - " + set.hashCode());
		
	}

}

HashSet class in java

Consolidated list of methods

The following table providess a consolidated view of all methods of HashSet.

Method Description
boolean add(E element) Appends given element to the HashSet.
boolean addAll(Collection c) Appends given collection of elements to the HashSet.
boolean remove(Object element) Removes the first occurence of the given element from the invoking HashSet.
boolean removeAll(Collection c) Removes all the elements those are in the specified collection from the invoking HashSet.
boolean removeIf(Predicate p) Removes all of the elements of the HashSet collection that satisfy the given predicate.
boolean retainAll(Collection c) Removes all the elements except those are in the specified collection from the invoking HashSet.
void clear( ) Removes all the elements from the HashSet.
int size( ) Returns the total number of elements in the invoking HashSet.
boolean isEmpty( ) Returns true if the HashSet is empty otherwise returns false.
boolean equals( ) Compares the specified object with invoking HashSet collection for equality.
boolean contains(Object element) Returns true if the HashSet contains given element otherwise returns false.
boolean containsAll(Collection c) Returns true if the HashSet contains all elements of given collection otherwise returns false.
int clone( ) Returns a copy of the invoking HashSet.
int hashCode( ) Returns the hash code of the invoking HashSet.
Object[ ] toArray( ) Returns an array of Object instances that contains all the elements from invoking HashSet.
Spliterator spliterator( ) Creates spliterator over the elements in a HashSet.
Iterator iterator( ) Returns an iterator over the elements in the HashSet. The iterator does not return the elements in any particular order.