Understanding HashSet Collection in Java

Feb 1, 2025

Lecture on HashSet Collection in Java

Key Concepts

  • Collections in Java

    • Collection is a root interface.
    • It is extended into multiple child interfaces: List, Set, and Queue.
  • Child Interfaces

    • List Interface: Implemented by ArrayList and LinkedList.
    • Set Interface: Focus of today’s lecture.

Set Interface

  • Set Interface Overview

    • Child interface of Collection.
    • Implemented by HashSet and LinkedHashSet.
  • Characteristics of Set Interface

    • Duplicates are not allowed.
    • Insertion order is not preserved.

HashSet

  • HashSet Class

    • A key class implementing Set interface.
    • Does not allow duplicate elements.
    • Does not maintain insertion order (elements stored in a random order).
    • Uses hashCode for element storage, enhancing search speed.
  • Key Features of HashSet

    • No duplicates.
    • No insertion order.
    • Unordered (no index concept).
    • Supports heterogeneous data and allows null values.
    • Efficient for search operations.

Creating HashSet Objects

  • Default Creation

    HashSet hs = new HashSet();
    // Default initial size: 16, Load factor: 0.75
    
  • Custom Initial Size and Load Factor

    HashSet hs = new HashSet(100);
    HashSet hs = new HashSet(100, 0.90f);
    
    • Initial size specifies storage capacity.
    • Load factor (fill ratio) determines when to increase object size (default 0.75).
  • Type Restriction

    HashSet<Integer> hs = new HashSet<>();
    // Stores only Integer objects
    

HashSet Operations

  • Adding Elements

    hs.add(value);
    hs.addAll(collection);
    
  • Removing Elements

    hs.remove(value);
    hs.removeAll(collection);
    
  • Checking Elements

    hs.contains(value);
    hs.containsAll(collection);
    hs.isEmpty();
    
  • Iteration

    • Using enhanced for loop or iterator.
    for (Object obj : hs) { ... }
    
    Iterator it = hs.iterator();
    while (it.hasNext()) { ... }
    

Limitations and Considerations

  • Sorting and Shuffling
    • Not directly possible; requires conversion to ArrayList.

Set Operations

  • Union, Intersection, Difference

    • Use methods like addAll(), retainAll(), removeAll() to perform set operations.
  • Subset Checking

    hs.containsAll(otherSet);
    

Conclusion

  • HashSet is valuable for scenarios needing fast search and no duplicates.
  • Understand capabilities and limits, like lack of direct sorting.