Skip to content
Related Articles

Related Articles

Improve Article

Generic Set In Java

  • Difficulty Level : Basic
  • Last Updated : 24 Oct, 2020
Geek Week

The Set interface is present in java.util package. It is basically a collection of objects with no duplicate objects that means there can be no two objects in a Set e1 and e2 such that e1.equals(e2) and at most one null element. It is an interface that models the mathematical set. This interface inherits the features from the Collection interface and places a feature that restricts the addition of duplicate elements on its own. The two interfaces which implement the Set interface are SortedSet and NavigableSet.

Generic Set In Java

In this image, the NavigableSet inherits/extends the SortedSet. Now, since the Set doesn’t retain the original order of insertion of elements, so NavigableSet provides the implementation of this interface to navigate the set. TreeSet which is an implementation of a Red-Black Tree and implements the NavigableSet.

Declaration of a Set Interface 

public interface Set extends Collection{


Syntax of a Set

Set< Integer > set = new HashSet< Integer >();

Now, this is targeted to only Integer datatype. i.e only Integer instances can be put in the Set. If we try to put something else in the Set, the compiler will give an error. The checks of generic type occur at the time of compilation.

Syntax of a Generic Set

Set< T > set = new HashSet< T >();

Above syntax is a generalized way to use T to show the generic behavior of a Set which means that T can be replaced with any non-primitive like Integer, String, Double, Character, or any user-defined type.

Adding Elements To A Generic Set

add() method is used to add elements in a Set. If the element already exists in the Set, it returns false

Set<Character> set = new HashSet<Character>();

Character ch = 'a';

The difference is, if we try to add something which is not a Character, then the compiler will show an error.

Iterating a Generic Set

1.  We can use an iterator to iterate over a Set.

Set<Integer> set = new HashSet<Integer>();
Iterator<Iterator> it = set.iterator();

  Integer aInt =;

2.  Another way to iterate over a Set is to use Generic for-loop.

Set<String> set = new HashSet<String>;

for(String st : set) {



// Java program to demonstrate Generic Set
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GenericSet {
    public static void main(String[] args)
        // create an instance of Set using
        // generics
        Set<Integer> set1 = new HashSet<Integer>();
        // Add elements
        // Create another set
        Set<String> set2 = new HashSet<String>();
        // Add elements
        // Iterate set1 using for-each loop
        for (Integer data : set1) {
            System.out.printf("Integer Value :%d\n", data);
        // Iterate set2 using iterator
        Iterator<String> stringIt = set2.iterator();
        while (stringIt.hasNext()) {
            System.out.printf("String Value :%s\n",
Integer Value :100
Integer Value :101
String Value :geeksforgeeks
String Value :generics

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :