top

Search

Java Tutorial

.

UpGrad

Java Tutorial

HashSet in Java

Introduction 

The Java Collections Framework's HashSet class in Java offers an implementation of the Set interface. It is used to keep an assortment of distinctive components. The hash table data structure on which HashSet is built enables effective insertion, deletion, and retrieval operations. The whole Java HashSet, including its definition, attributes, hierarchy, constructors, methods, and use examples, are covered in this article.

Overview 

Java's HashSet class is frequently used to represent an element-free, unordered collection. It implements the Set interface and extends the AbstractSet class. A hash table, which is used by HashSet to hold its items, offers constant-time performance for operations like add, delete, and contain. The sequence of the components is not fixed and is subject to vary over time.

What is HashSet in Java? 

Java's HashSet class implements the Set interface and offers a quick way to store distinct components. It stores and retrieves items using a hash table on the inside. Based on their hash codes, items may be quickly accessed using the hash table, which offers constant-time performance for simple operations. Here is an illustration of how to use HashSet:

javaCopy code 

import java.util.HashSet; 
public class HashSetExample { 
    public static void main(String[] args) { 
        HashSet<String> fruits = new HashSet<>(); 
         
        fruits.add("Apple"); 
        fruits.add("Banana"); 
        fruits.add("Orange"); 
        fruits.add("Apple"); // Duplicate element 
         
        System.out.println(fruits); 
    } 
}  

Output: 

csharpCopy code 
[Orange, Banana, Apple]  

In the illustration, a HashSet called "fruits" is created, and numerous components are added to it. It is important to note that HashSet does not accept duplicates. Therefore when we attempt to add the element "Apple," it is disregarded. The output shows the elements in an unordered manner. 

Properties of HashSet 

Unique Elements: HashSet does not allow duplicate elements. It ensures that each element in the set is unique based on the equals() method. 

Unordered Collection: HashSet does not guarantee any specific order for its elements. The order may change over time and is influenced by the internal hash table structure. 

Fast Performance: HashSet provides constant-time performance for basic operations like add, remove, and contain. The hash table data structure enables efficient retrieval and manipulation of elements. 

Difference between List and Set 

List and Set are both interfaces in the Java Collections Framework, but they have some fundamental differences. 

Duplicate Elements: List allows duplicate elements, whereas Set does not. You can have multiple occurrences of the same element in a List, while a Set ensures uniqueness by rejecting duplicates. 

Order of Elements: The list preserves the order of elements and allows positional access. You can access elements by their index in a List. On the other hand, Set does not maintain any specific order. The order of elements in a Set may change over time. 

Implementation: ArrayList and LinkedList are two implementations of the List interface that provide various performance and feature trade-offs. HashSet, one of the many implementations of Set, offers quick performance for fundamental operations at the expense of unordered items.

Hierarchy of HashSet Class 

The HashSet class in Java is part of the Java Collections Framework and inherits properties and methods from several classes and interfaces. The class hierarchy of HashSet is as follows: 

markdownCopy code 
java.lang.Object 
└── java.util.AbstractCollection 
     └── java.util.AbstractSet 
          └── java.util.HashSet  

The HashSet class directly extends the AbstractSet class, which provides a skeletal implementation of the Set interface. AbstractSet, in turn, extends the AbstractCollection class, which serves as a base implementation for various collection classes. 

HashSet Class Declaration 

The declaration of the HashSet class in Java is as follows: 

javaCopy code 

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable { 
    // Class implementation 
}  

Here, E represents the type of elements that the HashSet will hold. The class implements the Set interface and extends the AbstractSet class, providing implementations for various methods. 

Constructors of Java HashSet class 

The HashSet class provides several constructors to create instances of HashSet with different initial capacities and load factors. Here are some commonly used constructors: 

HashSet(): Constructs an empty HashSet with the initial default capacity (16) and load factor (0.75). 

Creates a HashSet from the items of the given collection using the function HashSet(Collection? extends E> c). 

HashSet(int initialCapacity): Creates a new HashSet that is empty and has a default load factor of 0.75 and the supplied initial capacity. 

An empty HashSet with the supplied initial capacity and load factor is created by using HashSet(int initialCapacity, float loadFactor). 

Let's look at an illustration of how to use these constructors: 

javaCopy code 

import java.util.HashSet; 
public class HashSetConstructorExample { 
    public static void main(String[] args) { 
        HashSet<String> fruits = new HashSet<>(); 
        
        fruits.add("Apple"); 
        fruits.add("Banana"); 
        fruits.add("Orange"); 
         
        HashSet<String> copySet = new HashSet<>(fruits); // Using the constructor HashSet(Collection<? extends E> c) 
         
        System.out.println(copySet); 
    } 
}  

Output: 

csharpCopy code 
[Banana, Apple, Orange]  

We establish a HashSet named "fruits" in the example and add entries to it. The constructor of the "copySet" HashSet, which accepts a Collection as an input, is then used to generate a new HashSet. This constructor creates a new HashSet with the same elements as "fruits." The output shows the elements in the "copySet" in an unordered manner. 

Methods of Java HashSet Class 

The HashSet class provides numerous ways to execute operations on the set. Among the often employed techniques are: 

Boolean add(E e): On the off chance that the given component isn't, as of now, present, it is added to the set. On the off chance that the component was added, it returns valid; in any case, it gets back misleading.

Boolean remove(Object o): Assuming the provided component is available, the boolean remove(Object o) capability eliminates it from the set. In the event that the component is erased, the reaction is valid; in any case, it is bogus.

Boolean contains(Object o): contains(Object o) returns valid, assuming the mentioned component is available in the set and bogus in any case.

int size(): Returns the number of things in the set utilizing the int size() capability.

void clear(): Removes all set components. 

Iterator<E> iterator(): Returns an iterator through the set's elements using the iteratorE> function.

Java HashSet Example 

Here's an example that demonstrates the usage of HashSet in Java: 

javaCopy code 

import java.util.HashSet; 
 
public class HashSetExample { 
    public static void main(String[] args) { 
        HashSet<String> names = new HashSet<>(); 
         
        names.add("John"); 
        names.add("Alice"); 
        names.add("Bob"); 
         
        System.out.println("HashSet: " names); 
        System.out.println("Size: " names.size()); 
        System.out.println("Contains 'Alice': " names.contains("Alice")); 
         
        names.remove("Bob"); 
         
        System.out.println("After removal: " names); 
    } 
}  

Output: 

yamlCopy code 
HashSet: [Alice, Bob, John] 
Size: 3 
Contains 'Alice': true 
After removal: [Alice, John]  

We build a HashSet named "names" in this example and add three members to it. Afterward, we publish the HashSet and its size and determine whether or not it contains the element "Alice." After that, we remove the element "Bob" and print the updated HashSet. 

  • Java HashSet example ignoring duplicate elements 

As HashSet does not allow duplicate elements, any attempt to add duplicates is ignored. Here's an example: 

javaCopy code 

import java.util.HashSet; 
 
public class HashSetDuplicatesExample { 
    public static void main(String[] args) { 
        HashSet<String> fruits = new HashSet<>(); 
         
        fruits.add("Apple"); 
        fruits.add("Banana"); 
        fruits.add("Orange"); 
        fruits.add("Apple"); // Duplicate element 
         
        System.out.println(fruits); 
    } 
}  

Output: 

csharpCopy code 
[Orange, Banana, Apple]  

In this example, we add the element "Apple" twice. However, HashSet ignores the duplicate element, and the output displays only one occurrence of "Apple" along with "Banana" and "Orange." 

  • Java HashSet example to remove elements 

You can remove elements from a HashSet using the remove() method. Here's an example: 

javaCopy code 

import java.util.HashSet; 
 
public class HashSetRemoveExample { 
    public static void main(String[] args) { 
        HashSet<String> colors = new HashSet<>(); 
         
        colors.add("Red"); 
        colors.add("Green"); 
        colors.add("Blue"); 
         
        System.out.println("Before removal: " colors); 
         
        colors.remove("Green"); 
         
        System.out.println("After removal: " colors); 
    } 
}  

Output: 

mathematicaCopy code 
Before removal: [Green, Blue, Red] 
After removal: [Blue, Red]  

In this example, we create a HashSet called "colors" and add three elements to it. We then print the HashSet before removal. After that, we remove the element "Green" using the remove() method and print the updated HashSet. 

  • Java HashSet from another Collection 

You may create a HashSet from another collection using the constructor that accepts a Collection as input. Here's an illustration:

javaCopy code 

import java.util.HashSet; 
import java.util.ArrayList;  
public class HashSetFromCollectionExample { 
    public static void main(String[] args) { 
        ArrayList<Integer> numbers = new ArrayList<>(); 
        numbers.add(1); 
        numbers.add(2); 
        numbers.add(3); 
         
        HashSet<Integer> numberSet = new HashSet<>(numbers); 
         
        System.out.println("HashSet: " numberSet); 
    } 
}  

Output: 

makefileCopy code 
HashSet: [1, 2, 3]  

In this model, three numbers are added to an ArrayList named "numbers" that was recently made. The constructor of a HashSet named "numberSet" that acknowledges an ArrayList as information is then used to create it. The components in the HashSet and the components in the ArrayList are shown in the result.

  • Java HashSet Example: Book 

Let's consider an example where we use a HashSet to store a collection of Book objects. Each Book object is defined by its title and author. We can add books to the HashSet and perform operations like adding, removing, and checking for the existence of a specific book. Here's an example: 

javaCopy code 

import java.util.HashSet; 
public class Book { 
    private String title; 
    private String author; 
     
    public Book(String title, String author) { 
        this.title = title; 
        this.author = author; 
    } 
     
    public String getTitle() { 
        return title; 
    } 
     
    public String getAuthor() { 
        return author; 
    } 
     
    @Override 
    public String toString() { 
        return title " by " author; 
    } 
     
    public static void main(String[] args) { 
        HashSet<Book> bookSet = new HashSet<>(); 
         
        Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald"); 
        Book book2 = new Book("To Kill a Mockingbird", "Harper Lee"); 
        Book book3 = new Book("1984", "George Orwell"); 
         
        bookSet.add(book1); 
        bookSet.add(book2); 
        bookSet.add(book3); 
         
        System.out.println("HashSet: " bookSet); 
        System.out.println("Size: " bookSet.size()); 
         
        Book bookToRemove = new Book("1984", "George Orwell"); 
        bookSet.remove(bookToRemove); 
         
        System.out.println("After removal: " bookSet); 
    } 
}  

Output: 

vbnetCopy code 
HashSet: [To Kill a Mockingbird by Harper Lee, 1984 by George Orwell, The Great Gatsby by F. Scott Fitzgerald] 
Size: 3 

After removal: [To Kill a Mockingbird by Harper Lee, The Great Gatsby by F. Scott Fitzgerald] 

In this example, a Book class is defined with characteristics for the author and title. To create a valid string representation of a Book object, we override the toString() function. We create three Book objects and add them to the HashSet called "bookSet." After printing the HashSet and its size, we create another Book object with the same title and author as "1984" and remove it from the HashSet using the remove() method. Finally, we print the updated HashSet. 

Java HashSet Features 

Unique Elements: By forbidding duplication, HashSet makes sure that each element is distinct. The equals() function is used to look for duplication. 

Fast Performance: HashSet provides constant-time performance for basic operations such as add, remove, and contains. This efficiency is achieved through the use of a hash table data structure. 

Unordered Collection: HashSet does not maintain any specific order for its elements. The order of elements can change over time due to resizing or other internal operations. 

Declaration of HashSet 

To declare a HashSet variable, you can use the following syntax: 

javaCopy code 

HashSet<E> set = new HashSet<>();  
Here, E represents the type of elements that the HashSet will hold. You can replace "E" with any valid Java class or interface. 

Internal Working of a HashSet 

HashSet utilizes a hash table information structure to store and recover passages. The hashCode() capability is utilized to decide a component's hash code when it is added to the HashSet. The component's stockpiling record in the hash is not set in stone by the hash code.

An impact happens when at least two passages share a similar hash code; in this situation, they are put away in a similar record utilizing a connected rundown. Once more, the hash code is determined while recovering a component, and the hash set looks for the component in the related record. Assuming that a connected rundown is available at that position, it crosses the rundown utilizing the equivalents() capability to find the matching component.

When to resize the internal hash table of a HashSet depends on its load factor. By default, the load factor is set to 0.75, which signifies that the HashSet will enlarge to accept more entries when it reaches 75% of its maximum capacity.

Implementation of HashSet in Java Doc 

The official Java documentation has information about HashSet's implementation. The internal workings, constructors, methods, and use examples of the HashSet class are covered in the Java documentation. 

Constructors of HashSet Class 

The HashSet class provides several constructors to create instances of HashSet with different initial capacities and load factors. Some commonly used constructors are: 

HashSet(): Constructs an empty HashSet with the initial default capacity (16) and load factor (0.75). 

HashSet(Collection<? extends E> c): Makes a HashSet from the things of the given assortment utilizing the capability HashSet(Collection? extends E> c).

HashSet(int initialCapacity): Makes another unfilled HashSet with a default load element of 0.75 and the underlying limit provided.

HashSet(int initialCapacity, float loadFactor): A void HashSet with the provided beginning limit and burden factor is made by utilizing HashSet(int initialCapacity, float loadFactor).

Performing Various Operations on HashSet 

  • Adding Elements in HashSet 

You can add elements to a HashSet using the add() method. Here's an example: 

javaCopy code 

import java.util.HashSet; 
 
public class HashSetAddExample { 
    public static void main(String[] args) { 
        HashSet<String> colors = new HashSet<>(); 
         
        colors.add("Red"); 
        colors.add("Green"); 
        colors.add("Blue"); 
         
        System.out.println(colors); 
    } 
}  

Output: 

mathematicaCopy code 
[Green, Blue, Red]  

In this example, we create a HashSet called "colors" and add three elements to it using the add() method. The elements are printed in an unordered manner. 

  • Removing Elements in HashSet 

You can remove elements from a HashSet using the remove() method. Here's an example: 

javaCopy code 

import java.util.HashSet; 
 
public class HashSetRemoveExample { 
    public static void main(String[] args) { 
        HashSet<String> colors = new HashSet<>(); 
         
        colors.add("Red"); 
        colors.add("Green"); 
        colors.add("Blue"); 
         
        System.out.println("Before removal: " colors); 
         
        colors.remove("Green"); 
         
        System.out.println("After removal: " colors); 
    } 
}  

Output: 

mathematicaCopy code 
Before removal: [Green, Blue, Red] 
After removal: [Blue, Red]  

In this example, we create a HashSet called "colors" and add three elements to it. We then print the HashSet before removal. After that, we remove the element "Green" using the remove() method and print the updated HashSet. 

  • Iterating through the HashSet 

You can iterate through the elements of a HashSet using an iterator or the enhanced for loop. Here's an example using the enhanced for loop: 

javaCopy code 

import java.util.HashSet; 
 
public class HashSetIterationExample { 
    public static void main(String[] args) { 
        HashSet<String> colors = new HashSet<>(); 
         
        colors.add("Red"); 
        colors.add("Green"); 
        colors.add("Blue"); 
         
        for (String color : colors) { 
            System.out.println(color); 
        } 
    } 
}  

Output

mathematicaCopy code 
Green 
Blue 
Red  

In this model, we make a HashSet called "varieties" and add three components to it. We utilize the improved circle to emphasize through the HashSet and print every component.

Performance of HashSet 

HashSet provides constant-time performance (O(1)) for basic operations such as add, remove, and contains. However, the performance can degrade under certain circumstances: 

Hash Function Collisions: A linked list will store multiple elements with the same hash code in the same index. This can lead to performance degradation when the linked list becomes long. 

Resizing: When a HashSet reaches its capacity threshold, it needs to be resized, which involves rehashing all the elements. This operation can be time-consuming for large HashSet instances. 

Object Equality Checks: When checking for the existence of an element or removing an element, HashSet uses the equals() method. If the equals() method is computationally expensive, and it can impact performance. 

Considering these factors when working with large HashSet instances or when dealing with complex hash functions and equality checks is important. 

Methods Used with HashSet 

HashSet provides various methods to perform operations on the set. Some commonly used methods are: 

  • Methods inherited from class java.util.AbstractSet: 

equals(Object o): compares the provided item to the set to determine equality. 

hashCode(): Provides the set's hash code value. 

  • Methods inherited from class java.util.AbstractCollection: 

addAll(Collection<? extends E> c): Adds every element in the supplied collection to the set with the syntax addAll(Collection? extends E> c). 

containsAll(Collection<?> c): The function containsAll(Collection? c) returns true if the set includes every member of the given collection. 

HashSet can also be used with methods specified in interfaces java.util.Collection, java.lang.Iterable, and java.util.Set. 

  • Methods declared in interface java.util.Collection: 

addAll(Collection<? expands E> c): Adds every one of the components in the predetermined assortment to the set.

containsAll(Collection<?> c): Returns valid, assuming the set contains every one of the components of the predetermined assortment.

  • Methods declared in interface java.lang.Iterable: 

iterator(): Returns an iterator over the components in the set.

  • Methods declared in interface java.util.Set: 

addAll(Collection<? extends E> c): Adds all the elements in the specified collection to the set. 

containsAll(Collection<?> c): Returns true if the set contains all the elements of the specified collection. 

equals(Object o): Compares the specified object with the set for equality. 

hashCode(): Returns the hash code value for the set. 

Conclusion 

HashSet in Java is a useful class that provides an implementation of the Set interface. It offers unique elements, fast performance, and data storage and retrieval flexibility. HashSet makes it simple to add, delete, and rapidly verify the presence of items. It is especially helpful when you need to keep a group of components without duplications. HashSet is a flexible data structure that may be used in a variety of contexts and applications where the need for uniqueness and speedy operations arises. By understanding its usage and capabilities, you can leverage HashSet effectively in your Java programs. 

FAQs 

1. Can HashSet contain duplicate elements? 

No, HashSet does not allow duplicate elements. If you try to add a duplicate element, it will be ignored. 

2. Does HashSet maintain the insertion order of elements? 

No, HashSet does not maintain any specific order for its elements. The order of elements can change over time due to resizing or other internal operations. 

3. Can we store null values in a HashSet? 

Yes, HashSet can store null values. It allows one null element. 

4. How does HashSet handle collisions? 

When multiple elements have the same hash code, HashSet stores them in the same index using a linked list. It traverses the linked list to find the matching element based on the equals() method. 

5. Is HashSet thread-safe? 

No, HashSet is not thread-safe. If multiple threads access a HashSet concurrently and at least one of them modifies the set, external synchronization is required to ensure thread safety. 

Leave a Reply

Your email address will not be published. Required fields are marked *