The HashSet class is part of the Java Collections Framework, providing a fast, unordered collection that doesn't allow duplicate elements. It is built on top of the HashMap, meaning it inherits the same time complexity benefits but focuses purely on element uniqueness. In this article, we’ll explore how HashSet works, what makes it unique, and why it's different from other collections.
A HashSet is:
// Parameterized constructor with initial capacity SetsetWithInitialCapacity = new HashSet(5); // Parameterized constructor using a collection Set setWithCollection = new HashSet(Arrays.asList(4, 4, 3)); // Default constructor with default capacity 16 Set set = new HashSet();
set.add(1); set.add(2); set.add(1); // Duplicate value is ignored System.out.println(set); // Output -> [1, 2]
If you need to replace duplicate values instead of ignoring them, HashSet won’t be the right choice. This is because it prioritizes element uniqueness.
// Parameterized constructor with initial capacity SetsetWithInitialCapacity = new HashSet(5); System.out.println(setWithInitialCapacity.size()); // Output -> 0
Even though the capacity of setWithInitialCapacity is 5, the size is 0 because size reflects the number of elements present in the set, not the initial capacity. You can think of capacity as the internal storage space, which adjusts as elements are added.
// Parameterized constructor using a collection SetsetWithCollection = new HashSet(Arrays.asList(4, 4, 3)); System.out.println(setWithCollection); // Output -> [3, 4] or [4, 3]
If you need to retain sorted elements, consider using a TreeSet, which ensures elements are arranged in ascending order.
In interviews, a common question is whether you can retrieve an index of an element in a HashSet. The answer is No, because HashSet uses a hashing mechanism to store elements, not an index-based structure like a list or an array.
Since HashSet is backed by a HashMap, it uses the keys of the map to store elements, while the values are irrelevant. This is why every element in a HashSet must be unique, just like the keys in a HashMap.
HashSet is a powerful tool when you need a fast, unordered collection that avoids duplicates. While it offers O(1) time complexity for most operations, it lacks features like sorting and indexing. For developers, knowing how HashSet relates to HashMap helps to understand its inner workings and make better use of the collections framework.
In the next post, we’ll explore a common interview question frequently asked in interviews to test candidates' knowledge of collections concepts.
Java Fundamentals
Array Interview Essentials
Java Memory Essentials
Happy Coding!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3