Java Set With Examples

Reading Time: 3 minutes

In Java , Set is defined as a collection of unordered elements . In this duplicate values cannot be stored . The Java Set interface extends the Collection interface . All the methods in the Collection Interface are available in Set interface . It can be implemented by HashSet, LinkedHashSet or TreeSet .

Creating Set object

Set<Obj> set = new HashSet<Obj> ()

Set Methods

1) ADD

This is used to add elements to the set .

Boolean add(type element) .

2) Clear

This method removes all the elements from the set .

Void clear() .

3) contains

This method is used to know the presence of an element in the set . It returns true or false depending on the presence of the element .

Boolean contains(Object element) .

4) isEmpty

It is used to identify the emptiness of the set . It returns True if the set is empty and return False if set is not empty .

Boolean isEmpty() .

5) remove

It removes an element from the Set .

Boolean remove(Object O).

6) Size

It returns the size of the set .

Int size() .

Java HashSet

HashSet class is implemented in the collection . HashSet does not maintains the insertion order . In HashSets elements are inserted on the basis of their hashcode.

Example

import java.util.*; 
class Demo{
public static void main(String[] args)
     { 
           // creating HashSet using Set . 

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

           // Adding elements into the HashSet by using add() method .

           h.add("India");
           h.add("Australia");
           h.add("South Africa");

           // Adding the duplicate element .

           h.add("India");

           // Displaying the HashSet .

           System.out.println(h);

           // Removing items from HashSet .

           h.remove("Australia");

           // Displaying the HashSet .

           System.out.println("Set after removing " + "Australia:" + h);
           System.out.println("Iterating over HashSet:");

           Iterator<String> i = h.iterator();

           // It holds true till there is a single element remaining in object .

           while (i.hasNext())
           System.out.println(i.next());

     }
 }

Output

[South Africa, Australia, India] .
Set after removing Australia:[South Africa, India] .
Iterating over set: .
South Africa .
India .

Java LinkedHashSet

The collection framework includes the LinkedHashSet class. It’s a HashSet that’s been ordered. Across all elements, it maintains a doubly Linked List. The insertion order of the elements in LinkedHashSet is preserved. The elements in the LinkedHashSet are iterated through in the same order as they were inserted.

Example

import java.util.*;
class Demo{ 
public static void main(String[] args)
      {
           // creating LinkedHashSet using Set . 

           Set<String> lh = new LinkedHashSet<String>();

           // Adding elements into the LinkedHashSet .
           // using add() .

           lh.add("India");
           lh.add("Australia");
           lh.add("South Africa");

           // Adding the duplicate element .

           lh.add("India");

           // Displaying the LinkedHashSet .

           System.out.println(lh);

           // Removing items from LinkedHashSet .
           // using remove() .

           lh.remove("Australia");

           // Displaying the LinkedHashSet .

           System.out.println("Set after removing " + "Australia:" + lh);
           System.out.println("Iterating over LinkedHashsSet:");

           Iterator<String> i = lh.iterator();

           // It holds true till there is a single element remaining in object .

           while (i.hasNext())
           System.out.println(i.next());

      } 
}

Output

[India, Australia, South Africa] .
Set after removing Australia:[India, South Africa] .
Iterating over set: .
India .
South Africa .

Java TreeSet

TreeSet class is implemented in the Collection framework . It uses tree data structure for storage . The objects of the TreeSet class are stored in sorted and ascending order .

Example

import java.util.*;
class Demo{
public static void main(String[] args)
     {
           // creating TreeSet using Set . 

           Set<String> ts = new TreeSet<String>();

           // Adding elements into the TreeSet .
           // using add() .
           
           ts.add("India");
           ts.add("Australia");
           ts.add("South Africa");

           // Adding the duplicate element .

           ts.add("India");

           // Displaying the TreeSet .

           System.out.println(ts);

           // Removing items from TreeSet .
           // using remove() .

           ts.remove("Australia");
           
           // Displaying the TreeSet .

           System.out.println("Set after removing " + "Australia:" + ts);
           System.out.println("Iterating over TreeSet:");

           Iterator<String> i = ts.iterator();

           // It holds true till there is a single element remaining in object .

           while (i.hasNext())
           System.out.println(i.next());

     }
}

Output

[Australia, India, South Africa] .
Set after removing Australia:[India, South Africa] .
Iterating over set: .
India .
South Africa .

Conclusion

In this blog we have learnt about the concept of Set in JAVA language . We implemented code for better understanding with suitable example .

I hope you have liked my blog .

Thanks! .

You can refer to the documentation :- https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html .

knoldus footer

Written by 

KRISHNA JAISWAL is Software Consultant Trainee at Knoldus. He is passionate about JAVA , MYSQL , having knowledge of C , C++ and much more. He is recognised as a good team player, a dedicated and responsible professional, and a technology enthusiast. He is a quick learner & curious to learn new technologies. His hobbies include reading Books , listening Music and playing Cricket .