The Set interface is a part of the Java Collections Framework and is a subinterface of Collection. It represents a collection that does not allow duplicate elements. The Set interface does not provide any additional methods beyond those specified in the Collection interface, but it adds the stipulation that duplicates are not allowed.
Set does not allow duplicate elements. Each element in a Set must be unique.Set interface does not guarantee the order of elements. The insertion order is not preserved.Set can contain at most one null element.Set interface inherits all the methods from the Collection interface.import java.util.HashSet;import java.util.Set;public class SetExample { public static void main(String[] args) { // Create a HashSet Set<String> set = new HashSet<>(); // Add elements to the set set.add("Apple"); set.add("Banana"); set.add("Cherry"); set.add("Apple"); // Duplicate element // Print the set System.out.println("Set: " + set); // Check if the set contains an element System.out.println("Contains 'Apple': " + set.contains("Apple")); // Remove an element set.remove("Banana"); System.out.println("After removing 'Banana': " + set); // Iterate over the set System.out.println("Iterating over set:"); for (String item : set) { System.out.println(item); } // Size of the set System.out.println("Size of set: " + set.size()); // Clear the set set.clear(); System.out.println("Is set empty after clear: " + set.isEmpty()); }}Set: [Apple, Cherry, Banana]
Contains 'Apple': true
After removing 'Banana': [Apple, Cherry]
Iterating over set:
Apple
Cherry
Size of set: 2
Is set empty after clear: true