The Set Interface in Java


Set is a collection that cannot contain duplicate elements. It models the mathematical set abstraction. HashSet, LinkedHashSet, and TreeSet are implementations of the Set interface.

Source Code

Set set = new HashSet();
set.add("Apple");
set.add("Banana");
set.add("Apple");
System.out.println(set.size()); // Outputs "2" because duplicates are not allowed

Use a Set when you need to ensure that no duplicates are stored, or when you need efficient lookup operations.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments