Set in Java Collections | Explain Set in Collections Framework?

 





In the Java Collections framework, the Set interface represents a collection of unique elements. It extends the Collection interface, so it inherits all of its methods.


The Set interface doesn't allow duplicates, which means that if you try to add an element to a set that already contains that element, the set will not be changed. This makes sets useful for representing a collection of distinct values.


Java provides several implementations of the Set interface, including HashSet, TreeSet, and LinkedHashSet. Each implementation has its own characteristics and performance characteristics, so you should choose the right set implementation based on your needs.


The basic operations that can be performed on a Set include adding elements, removing elements, checking if an element is present in the set, and iterating over the elements in the set. You can also get the number of elements in the set using the size() method.


In Java, there are several implementations of the Set interface:


HashSet: A HashSet is a Set implementation that uses a hash table for storage. It provides fast performance for add, remove, and contains operations. It does not maintain the order of elements, so the elements are returned in a random order. It's an efficient choice when you need a set with good performance and don't care about the order of the elements.


TreeSet: A TreeSet is a Set implementation that uses a red-black tree for storage. It provides fast performance for add, remove, and contains operations and maintains the elements in sorted order. This makes it a good choice when you need a set that maintains the elements in a specific order, such as ascending or descending.


LinkedHashSet: A LinkedHashSet is a Set implementation that uses a hash table and a linked list for storage. It provides fast performance for add, remove, and contains operations and maintains the elements in the order in which they were added. This makes it a good choice when you need a set that maintains the elements in the order in which they were added, but also provides fast performance.


import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
 
public class Main {
    public static void main(String[] args) {
        // Create a HashSet of Strings
        Set<String> hashSet = new HashSet<String>();
        hashSet.add("Apple");
        hashSet.add("Banana");
        hashSet.add("Cherry");
        System.out.println("HashSet: " + hashSet);
 
        // Create a TreeSet of Strings
        Set<String> treeSet = new TreeSet<String>();
        treeSet.add("Apple");
        treeSet.add("Banana");
        treeSet.add("Cherry");
        System.out.println("TreeSet: " + treeSet);
 
        // Create a LinkedHashSet of Strings
        Set<String> linkedHashSet = new LinkedHashSet<String>();
        linkedHashSet.add("Apple");
        linkedHashSet.add("Banana");
        linkedHashSet.add("Cherry");
        System.out.println("LinkedHashSet: " + linkedHashSet);
    }
}
 



When you run the above code, it will output the following:



HashSet: [Cherry, Banana, Apple]
TreeSet: [Apple, Banana, Cherry]
LinkedHashSet: [Apple, Banana, Cherry]



In this example, we create a HashSet, TreeSet, and LinkedHashSet and add the same elements to each set. When we print the sets, you can see that the HashSet returns the elements in a random order, the TreeSet returns the elements in sorted order, and the LinkedHashSet returns the elements in the order in which they were added.

Post a Comment

Post a Comment (0)

Previous Post Next Post