Understanding Java HashSet for DSA

A HashSet in Java is a part of the Java Collections Framework and provides a way to store unique elements. It is part of the java.util package and implements the Set interface. The HashSet class does not allow duplicate elements, allows at most one null element, and does not guarantee the order of its elements over time.

Key Characteristics

  1. Unique Elements: Stores unique elements; duplicate elements are not allowed.

  2. At Most One Null Element: Allows at most one null element.

  3. Unordered: Does not guarantee any specific order of the elements.

  4. Not Synchronized: The HashSet itself is not synchronized. For concurrent access, use CopyOnWriteArraySet or synchronize it externally.

Constructor

  1. Default Constructor: This creates an empty HashSet with the default initial capacity (16) and the default load factor (0.75).

     HashSet<String> set = new HashSet<>();
    
  2. Constructor with Initial Capacity: This creates an empty HashSet with the specified initial capacity and the default load factor (0.75).

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

     HashSet<String> set = new HashSet<>(20, 0.8f);
    
  4. Constructor with Collection: This creates a new HashSet with the same elements as the specified Collection.

     List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
     HashSet<String> set = new HashSet<>(list);
    

Common Methods

  1. add(E e): Adds the specified element to this set if it is not already present.

     hashSet.add("element1");
    
  2. contains(Object o): Returns true if this set contains the specified element.

     boolean contains = hashSet.contains("element1");
    
  3. remove(Object o): Removes the specified element from this set if it is present.

     hashSet.remove("element1");
    
  4. size(): Returns the number of elements in this set.

     int size = hashSet.size();
    
  5. isEmpty(): Returns true if this set contains no elements.

     boolean empty = hashSet.isEmpty();
    
  6. clear(): Removes all of the elements from this set.

     hashSet.clear();
    

Iteration

HashSet can be iterated using various methods such as for-each loop, iterators, and streams.

for (String item : hashSet) {
    System.out.println(item);
}    

Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

hashSet.forEach(System.out::println);

Performance Considerations

  1. Time Complexity: The average time complexity for add, remove, and contains operations is O(1). However, in the worst case (due to hash collisions), it can be O(n).

  2. Load Factor: The load factor determines when the capacity of the HashSet is increased. A higher load factor means less space overhead but potentially slower access times.

  3. Initial Capacity: The initial capacity of the HashSet determines the number of buckets when the HashSet is created. Choosing an appropriate initial capacity can reduce the need for resizing.

Thank you for reading!

You can support me by buying me a book.

0
Subscribe to my newsletter

Read articles from Vineeth Chivukula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vineeth Chivukula
Vineeth Chivukula

There's this guy who's mad about editing and programming. It's his jam, you know?