Understanding Java HashSet

Introduction

link to this section

In Java, HashSet is a collection class that implements the Set interface, providing a way to store unique elements. Unlike lists, sets do not allow duplicate elements. HashSet uses a hash table under the hood to store its elements, providing constant-time performance for basic operations like add , remove , and contains . In this tutorial, we'll dive deep into the HashSet class in Java, exploring its features, methods, and best practices.

HashSet Features

link to this section
  • Unordered Collection : HashSet does not maintain the order of its elements. The elements are stored in a hash table, which means they are not stored in any specific order.

  • No Duplicates : HashSet does not allow duplicate elements. If you attempt to add a duplicate element to a HashSet , the operation will be ignored, and the set will remain unchanged.

  • Fast Operations : Basic operations like adding, removing, and checking for the presence of an element ( contains ) have constant-time complexity on average, making HashSet efficient for large data sets.

Creating a HashSet

link to this section

You can create a HashSet in Java using one of the following constructors:

  1. Default Constructor : Creates an empty HashSet with an initial capacity of 16 and a load factor of 0.75.

    HashSet<String> set = new HashSet<>(); 
  2. Constructor with Initial Capacity : Creates an empty HashSet with the specified initial capacity.

    int initialCapacity = 20; 
    HashSet<String> set = new HashSet<>(initialCapacity); 
  3. Constructor with Initial Capacity and Load Factor : Creates an empty HashSet with the specified initial capacity and load factor.

    int initialCapacity = 20; 
    float loadFactor = 0.8f; 
    HashSet<String> set = new HashSet<>(initialCapacity, loadFactor); 

Basic HashSet Operations

link to this section

Adding Elements

You can add elements to a HashSet using the add method. If the element is already present in the set, the add method will return false , indicating that the element was not added.

HashSet<String> set = new HashSet<>(); 
set.add("apple"); 
set.add("banana"); 
set.add("apple"); // This element will not be added 

Removing Elements

You can remove elements from a HashSet using the remove method.

HashSet<String> set = new HashSet<>(); 
set.add("apple"); 
set.add("banana"); 
set.remove("apple"); 

Checking for Element Existence

You can check if an element exists in a HashSet using the contains method.

HashSet<String> set = new HashSet<>(); 
set.add("apple"); 
set.add("banana"); 
boolean containsApple = set.contains("apple"); // true 
boolean containsOrange = set.contains("orange"); // false 

Conclusion

link to this section

HashSet is a versatile and efficient collection class in Java for storing unique elements. Its constant-time performance for basic operations makes it ideal for applications where fast element insertion, removal, and lookup are required. By understanding its features and methods, you can leverage HashSet effectively in your Java projects. Happy coding!