Set Interface implemented classes In Java

Set in Java

The second interface is Set which is extend the collection. Set interface is a unique interface and not maintain any order. Means In the Set interface duplicate values cannot be stored.
  • Set is an another interface which continues Collection. It holds an unordered collection of objects in which duplicate/repeated values not allowed.
  • Implementation classes of the set interface are HashSet, LinkedSet or TreeSet.
// Java code for adding elements in Set

import java.util.*;

public class Set_example{

    public static void main(String[] args){

        // Set deonstration using HashSet

        Set<String> hashSet = new HashSet<String>();

        hashSet .add("Tom");
        hashSet .add("Jerry");
        hashSet .add("Tom");

        System.out.print("Set output without the duplicates values : ");

        System.out.println(hashSet );

    }

}

 

Output:

Set output without the duplicates[Tom, Jerry]

Note: As we know that duplicate now allow in set “Tom” is ignored in the final output, Set interface doesn’t allow duplicate entries.

 
Now we will see some of the implemented classes and their Difference.

 

Related Articles

post a comment