What is the Set interface?
The Set interface in Java is your go-to when you need a collection that stores unique elements only. No duplicates, no index-based access.
-Set extends the Collection interface and lives in java.util. Its defining contract: it will never contain duplicate elements.
-Internally, "duplicate" is determined by equals() and hashCode() — so you must override both when using custom objects.
1.HashSet:
Use HashSet when you only care about uniqueness and O(1) average-case performance, and don't need a predictable iteration order.
O(1) avg -Backed by a HashMap. Fastest for add/remove/contains. No ordering guarantee whatsoever.
Example:
Set < String > fruits = new HashSet <>(); fruits . add ( "Apple" ); fruits . add ( "Mango" ); fruits .…