Core Java Concepts and Syntax

Understand HashSet in Java

Understand HashSet in Java

What Is Hashset In Java?

In Java, HashSet extends the Abstract class and implements the Set interface. The tool is helpful because it allows you to store unique items and access them continuously (on average).

You can not store duplicate values inside HashSet. To insert objects into the HashSet, you need to use a hash code; thus, you can store and retrieve HashSet elements in any random order. If you prefer storing and retrieving elements in sorted order, you can use TreeSet.

The most helpful advantage of HashSet is that you can store Null elements in HashSet. The HashSet class is not thread-safe; as a result, multiple threads cannot synchronize the results if they change the same HashSet instance.

Here are some important points about Java HashSet:

  • Hashing is the mechanism used by HashSet to store the elements.
  • A HashSet only contains unique elements.
  • A HashSet can contain a null value.
  • A HashSet is a non-synchronized class.
  • HashSet does not maintain insertion order. Instead, elements are inserted based on their hashcodes.
  • In terms of search operations, HashSets are the best option.

HashSet Declaration

The keyword HashSet is used to define a HashSet in Java. HashSet is defined as:

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

In a HashSet, E represents the type of elements stored.

Commonly used methods

#1. add(element):

It adds the given element in the HashSet if no such element already exists, otherwise, it doesn't.

alphabets.add("a");
alphabets.add(null);

#2. remove(element):

Using this method, you can remove an element from the HashSet.

alphabets.remove("a");

#3. clear():

This method is used to clear all the values stored in the HashSet.

alphabets.clear();

#4. size():

By using this method, you can find out how many elements are present in a HashSet.

alphabets.size();

#5. contains():

Using this method, you can determine whether a certain element is present in a HashSet. The process usually takes O(1) time.

alphabets.contains("a");

#6. isEmpty():

Using this method, you can check whether the HashSet contains any elements. If the method finds no elements, it returns true.

alphabets.isEmpty();

#7. toArray() :

The HashSet is converted to an array using this method. Each element of the HashSet gets copied into a new array.

String[] array = new String[alphabets.size()];
alphabets.toArray(array);

Code Implementation

Here is a simple implementation of a HashSet in java:

import java.util.HashSet;

class Main {
  public static void main(String[] args) {
    // A HashSet is created to store String values
    HashSet<String> hash_set = new HashSet<String>();
    // HashSet elements can be added using add()
    hash_set.add("a");
    hash_set.add("d");
    hash_set.add("x");
    hash_set.add("y");
    hash_set.add("z");
    // Displaying Unordered HashSet
    System.out.println("HashSet: " + hash_set);
    // Retrieving HashSet size
    int size = hash_set.size();
    System.out.printf("Size of HashSet is: %d \n", size);
    // Removing the element
    hash_set.remove("x");
    // After removal, display the HashSet   
System.out.println("HashSet after removal: " + hash_set);

    }
}

Output

HashSet: [a, d, x, y, z]
Size of HashSet is: 5
HashSet after removal: [a, d, y, z]

write your code here: Coding Playground