Set interface in java

Java Set interface

Set is part of collection framework in java .

Set is child interface of collection interface. And set is implemented by HashSet,LinkedHashSet or TreeSet.

If we want to represent group of individual object as single entity where duplicates are allowed  and insertion order are not preserved then we should go for set.

Set allows you to add at most one null element only.

Set interface got one default method in Java 8: spliterator.

Unlike List and arrays, Set does NOT support indexes or positions of it’s elements.

Some Methods to we use in set interface:

Modifier and TypeMethodDescription
booleanadd​(E e)Adds the specified element to this set .
voidclear()Removes all of the elements from this set.
booleancontains​(Object o)Returns true if this set contains the specified element.
booleanisEmpty()Returns true if this set contains no elements or empty.
Iterator<E>iterator() Returns an Iterator object for the collection, which may be used to retrieve an object.
booleanremove​(Object o)Removes the specified element from this set if it is present.
intsize()Returns the number of elements in this set (its cardinality).
Spliterator<E>spliterator() It generates a Spliterator over the specified elements in the collection.

Set has its implementation in various classes like HashSet, TreeSet, LinkedHashSet. Following is an example to explain Set functionality.

 import java.util.HashSet;
 import java.util.Set;
 import java.util.Spliterator;
 import java.util.TreeSet;
 import java.util.SortedSet;
 public class CreateSetDemo {
     public static void main(String[] args) {
         // Creating a LinkedList
           Set players = new HashSet<>();     
// Adding new elements to the end of the set using add() method.
    // obeserve carefully above line of code we add each player name twice  but in output removes dublicate elements. 
    System.out.println("Initial initial set : " + players);//
    // to find this size of set
    System.out.println("size of set"+players.size());
    //To chech element is available into set or not
    System.out.println("eleemnt in set"+players.contains("Ram"));
    System.out.println("size of set"+players.contains("Virat"));
    //To check set is empty or not 
    System.out.println("//To check set is empty or not"+players.isEmpty());

   // System.out.println(players.spliterator());
    Spliterator<String> spliterator = players.spliterator();
   //sorting element using sortedset
        SortedSet<String> sortedSet=new TreeSet<>(players);
        System.out.println("The sorted list is:");

         System.out.println("The First element of the set is: "+ (String)sortedSet.first());
         System.out.println("The last element of the set is: "+ (String)sortedSet.last());


Initial initial set : [Rohit, Rahul, Suresh, Virat]
 size of set4
 eleemnt in setfalse
 size of settrue
 //To check set is empty or notfalse
 The sorted list is:
 [Rahul, Rohit, Suresh, Virat]
 The First element of the set is: Rahul
 The last element of the set is: Virat


Java LinkedList tutorial with example

Java ArrayList Tutorial with Examples

List Interface in java

Collection interface in java

Leave a Reply