Loading

Q1. What are the characteristics of a HashSet in Java?
HashSet is part of the Java Collections Framework. It:
  • Implements the Set interface.
  • Stores only unique elements.
  • Does not maintain insertion order.
  • Allows one null element.





Q2. How does HashSet internally work?

HashSet is backed by a HashMap. The elements are stored as keys in the map with a constant dummy value. It uses hashCode() and equals() to avoid duplicates.






Q3. What is the time complexity of basic operations in HashSet?

In general:

  • add(), remove(), and contains() O(1) (average case).
  • In the worst case (hash collisions), these operations can degrade to O(n).





Q4. Can HashSet store duplicate elements or multiple nulls?

No, HashSet cannot store duplicates. It allows only one null value.






Q5. What are the main differences between HashSet and LinkedHashSet?
  • HashSet: No order of elements.
  • LinkedHashSet: Maintains insertion order using a linked list internally.