Java HashSet Class
In Java, the HashSet
class is one of the most commonly used implementations of the Set
interface. It provides a collection that does not allow duplicate elements and does not guarantee any specific order. It is backed by a hash table, making it a highly efficient choice for situations where uniqueness is important, but ordering is not.
The HashSet
class is part of the java.util
package and implements the Set
interface. It is designed to store unique elements, and it uses a hash table for its internal data structure. The HashSet
class allows for constant time performance for basic operations like add()
, remove()
, and contains()
, assuming the hash function disperses the elements properly.
Key Characteristics of HashSet:
HashSet
does not allow duplicate elements.HashSet
are not stored in any predictable order. It does not guarantee the order of elements.null
element.Here are some of the main features of HashSet
:
HashSet
are stored in an unordered manner. If you need to maintain the insertion order, use LinkedHashSet
.add()
, remove()
, contains()
) typically run in constant time O(1).HashSet
ensures that no duplicate elements are stored, meaning the collection will automatically eliminate any repeated values.HashSet
is not thread-safe by default. If you need a thread-safe version, you can use Collections.synchronizedSet()
to wrap it, or consider using a ConcurrentHashMap
.The HashSet
class inherits methods from the Set
interface. Below are some commonly used methods:
Adds an element to the set. If the element already exists, it will not be added (i.e., duplicates are ignored).
boolean add(E e);
Removes the specified element from the set.
boolean remove(Object o);
Checks if the set contains the specified element.
boolean contains(Object o);
Returns the number of elements in the set.
int size();
Checks if the set is empty.
boolean isEmpty();
Removes all elements from the set.
void clear();
Returns an iterator over the elements in the set.
Iterator<E> iterator();
Performs the given action for each element in the set.
void forEach(Consumer<? super E> action);
Here is an example demonstrating how to use HashSet
in Java:
import java.util.*;
public class HashSetExample {
public static void main(String[] args) {
// Create a HashSet to store unique elements
Set<String> fruits = new HashSet<>();
// Add elements to the set
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // Duplicate, will not be added
// Check if a specific element exists
System.out.println("Contains 'Banana': " + fruits.contains("Banana")); // true
// Print all elements in the set
System.out.println("Fruits in the HashSet: " + fruits); // Output: [Apple, Banana, Orange]
// Remove an element
fruits.remove("Orange");
System.out.println("Fruits after removal: " + fruits); // Output: [Apple, Banana]
// Get the size of the set
System.out.println("Size of the set: " + fruits.size()); // Output: 2
// Check if the set is empty
System.out.println("Is the set empty? " + fruits.isEmpty()); // false
// Iterate over the set using forEach
fruits.forEach(fruit -> System.out.println(fruit)); // Output: Apple, Banana (order may vary)
// Clear the set
fruits.clear();
System.out.println("Fruits after clearing: " + fruits); // Output: []
}
}
Output:
Contains 'Banana': true
Fruits in the HashSet: [Apple, Banana, Orange]
Fruits after removal: [Apple, Banana]
Size of the set: 2
Is the set empty? false
Apple
Banana
Fruits after clearing: []
add()
ensures that Apple is only added once, even though it was attempted twice.contains()
checks if the set contains the specified element, like Banana
.remove()
removes Orange
from the set.size()
gives the number of elements currently in the set.isEmpty()
checks if the set is empty.forEach()
allows iteration over the elements using a lambda expression.clear()
clears all elements in the set.You should consider using HashSet
in Java when:
Feature | HashSet |
LinkedHashSet |
TreeSet |
---|---|---|---|
Order | No order. | Maintains insertion order. | Sorts elements in ascending order. |
Performance | O(1) for add, remove, contains (average). | O(1) for add, remove, contains (average). | O(log n) for add, remove, contains. |
Null Elements | Allows at most one null element. |
Allows at most one null element. |
Does not allow null elements. |
Use Case | Best for collections where order doesn’t matter. | Best for collections where order matters (insertion order). | Best for collections that require sorting. |