Set-Interface
The Set interface is a part of the Java Collection Framework and is used to store a collection of unique elements. Unlike List, it does not allow duplicate values and does not maintain insertion order.
Block Diagram of Set Interface
- << >> : Denotes an Interface
- < > : Denotes a Class
- ----------> : Represents Implementation
Implementation Classes of Set Interface
1. HashSet- Uses HashTable for storage.
- Does not maintain insertion order.
- Provides constant-time performance for add, remove, and search operations.
2. LinkedHashSet
- Maintains insertion order.
- Slower than HashSet due to ordering maintenance.
3. TreeSet
- Stores element in sorted order (ascending by default).
- Imlements NavigableSet, allowing range-based operations.
Key Point
- Set is a type of collection that stores only unique elements. It doesn't allow duplicate elements.
- It doesn't have Index i.e. elements stored randomly.
- The Set doesn't maintain the order of insertion.
- The elements of the Set can be accessed either by using enhanced for loop or Iterator.