Using Set Interface in Java


The Set interface is used to store unique elements, making it useful for removing duplicates from a collection.

Source Code

import java.util.HashSet;
import java.util.Set;

public class TestSet {
    public static void main(String[] args) {
        Set set = new HashSet();
        set.add("Hello");
        set.add("World");
        set.add("Hello"); // Duplicate, not added
        System.out.println(set); // Observe the unique elements
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments