Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
data structure
hashset
set
communitycreator

HashSet in Java

Stuti Jain

HashSet is a class that extends AbstractSet and implements the Set interface in Java. It is a very useful tool that allows you to store unique items and access them in constant time (on average). No duplicate values are stored.

widget

Objects are inserted using hash code; therefore, it is not stored in an ordered fashion and the elements will be returned in any random order. But, if you need sorted storage, then another collection, such as TreeSet, could be used. Null elements are also allowed in HashSet.

HashSets are not thread-safe; so, if multiple threads modify the same HashSet instance, the results will not be synchronized.

HashSet declaration

In Java, a HashSet is declared using the keyword HashSet. This is followed by angle brackets < > that contain the data type of the data being stored. These angle brackets are followed by the name of the HashSet.

The general syntax for declaring a HashSet is as follows:

import java.util.*; 
class DemoHashSet {
    public static void main(String args[]) {
    HashSet<String> alphabets = new HashSet<String>();
    }
}

Commonly used methods

add(element) : This method is used to add the given element in the HashSet. The element is added if no such element already exists, else it is not added.

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

remove(element) : This method is used to remove the given element from the HashSet.

alphabets.remove("a");

clear() : This method is used to empty the HashSet. All the stored values in the HashSet are cleared by this method.

alphabets.clear();

size() : This method is used to print the size of the HashSet. This returns the number of elements present in the HashSet.

alphabets.size();

contains() : This method is used to check whether a particular element is present in the given HashSet or not. This usually happens in O(1)O(1) time.

alphabets.contains("a");

isEmpty() : This method is used to check whether or not the HashSet contains an element. It returns true if no element is found in HashSet.

alphabets.isEmpty();

toArray() : This method is used to convert the HashSet to an Array. Each and every element of the HashSet is copied to a newly formed array.

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

Implementation

Let’s take a look at a simple implementation of a HashSet:

import java.util.HashSet;

class Main {
  public static void main(String[] args) {
    // Creating HashSet to store String values
    HashSet<String> hash_set = new HashSet<String>();
    // Adding elements into HashSet usind add()
    hash_set.add("a");
    hash_set.add("b");
    hash_set.add("c");
    hash_set.add("d");
    // Displaying the HashSet Unordered
    System.out.println("HashSet: " + hash_set);
    // Extracting size of HashSet
    int size = hash_set.size();
    System.out.printf("Size of HashSet is: %d \n", size);
    // Remove an element
    hash_set.remove("b");
    // Display HashSet after removal
    System.out.println("HashSet after removal: " + hash_set);

    }
}

RELATED TAGS

java
data structure
hashset
set
communitycreator
RELATED COURSES

View all Courses

Keep Exploring