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.

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';
set.add(ch);
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();
while(it.hasNext()){
Integer aInt = iterator.next();
}
2. Another way to iterate over a Set is to use Generic for-loop.
Set<String> set = new HashSet<String>;
for(String st : set) {
System.out.println(st);
}
Example:
Java
// 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
set1.add(100);
set1.add(Integer.valueOf(101));
// Create another set
Set<String> set2 = new HashSet<String>();
// Add elements
set2.add("geeksforgeeks");
set2.add("generics");
// 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",
stringIt.next());
}
}
}
OutputInteger Value :100
Integer Value :101
String Value :geeksforgeeks
String Value :generics
Similar Reads
Generics in Java Generics means parameterized types. The idea is to allow a type (like Integer, String, etc., or user-defined types) to be a parameter to methods, classes, and interfaces. Generics in Java allow us to create classes, interfaces, and methods where the type of the data is specified as a parameter. If w
10 min read
Generic Map In Java Java Arrays store items in an ordered collection and the values can be accessed using the index(an integer). Whereas HashMap stores as a Key/ Value pair. Using HashMap, we can store the items or values and these values can be accessed by indexes/ keys of any type be it Integer, String, Double, Chara
3 min read
Generic Class in Java Java Generics was introduced to deal with type-safe objects. It makes the code stable.Java Generics methods and classes, enables programmer with a single method declaration, a set of related methods, a set of related types. Generics also provide compile-time type safety which allows programmers to c
4 min read
Generic For Loop in Java When we know that we have to iterate over a whole set or list, then we can use Generic For Loop. Java's Generic has a new loop called for-each loop. It is also called enhanced for loop. This for-each loop makes it easier to iterate over array or generic Collection classes. In normal for loop, we wri
4 min read
Restrictions on Generics in Java In Java, Generics are the parameterized types that allow us to create classes, interfaces, and methods in which the type of data they are dealing with will be specified as a parameter. This ensures the type safety at the compilation time. Syntaxclass class_name<T> { //Body of the class } Here,
5 min read