Java programming language provides the Set structure which implements array or list like collections. It is an unordered collection of objects where duplicate values cannot be stored. Objects can be generally string, integer, or complex objects like classes. HashSet, LinkedHashSet, and TreeSet structures implement the Set as base or ancestor.
Create Set
We will start by creating a set. We need to import the java.util.*
in order to use Set. We will create a string set that can be added with add()
function.

// Set Examples In Java Programming Language
import java.util.*;
public class Set_Example
{
public static void main(String[] args)
{
// Set deonstration using HashSet
Set<String> mySet = new HashSet<String>();
mySet.add("Poftut");
mySet.add("İsmail");
mySet.add("Ahmet");
mySet.add("Ali");
mySet.add("Ali");
System.out.println(mySet);
}
}
When we run the code given the added hash elements like below. We can see in the code that Ali
is added twice but printed once because duplicate entries cannot be stored as set logic.
$ java Set.java

Create Integer Set
We can create an integer set. In this example, we will use digits but some of the multiple times. Multiply added digits will be stored as single. We need to create Integer
sets like below.
// Set Examples In Java Programming Language
import java.util.*;
public class Set_Example
{
public static void main(String[] args)
{
// Set deonstration using HashSet
Set<Integer> mySet = new HashSet<Integer>();
mySet.add(1);
mySet.add(1);
mySet.add(2);
mySet.add(2);
mySet.add(3);
System.out.println(mySet);
}
}
We have added 1 and 2 multiple times but they are store only one time.

Create a String Set
We can also create a string set. Actually, we have created a string set in the previous examples but I wanted to express string set explicitly. Keep in mind that we need to set the set type as a string like below.
// Set Examples In Java Programming Language
import java.util.*;
public class Set_Example
{
public static void main(String[] args)
{
// Set deonstration using HashSet
Set<String> mySet = new HashSet<String>();
mySet.add("Poftut");
mySet.add("İsmail");
mySet.add("Ahmet");
mySet.add("Ali"); mySet.add("Ali");
System.out.println(mySet);
}
}
Remove Element From Set
After adding some elements we may need to remove some of them. We can use the remove()
function by providing the object like string, integer, etc. In this example, we will remove the element Ali
from the set mySet
.
mySet.remove("Ali");
Clear Set or Remove All Elements
We can also remove all elements from the given set with a single function. We can use clear()
function which will clear the set. In this example, we will clear all elements of mySet
.
mySet.clear();
Get Size of Set
A set can store a lot of elements. We can get the size of the set or the element count of the set with the size()
function like below.
mySet.size();
Union of Set
Union means joining multiple sets together. Sets cannot hold duplicate items we can union sets by adding them together. This will not create duplicate items in the new union set. We will use the addAll()
function and the set we want to add. We will create a set named unionSet
and add two sets named mySet1
, mySet2
.
Set unionSet=new HashSet();
unionSet.addAll(mySet1);
unionSet.addAll(mySet2);
The Intersection of Two Sets
The intersection of sets is a very popular and useful function to get common elements of two sets. We can use retainAll()
function with the first set and provide the second set. In this example, we will see the intersection of sets mySet1 and mySet2 into a set named intersectionSet.
Set intersectionSet=new HashSet(mySet1);
intersectionSet.retainAll(mySet1);
The Difference Between Two Sets
The difference is the elements that do not exist in the other set. We can find the difference from mySet1 from mySet2 by removing all elements of the mySet1 of mySet2 like below.
mySet2.removeAll(mySet1);
Check If Set Is Empty
We can check if the set is empty in different ways. But in this case, we will use the size()
function if the set is empty. If the size is 0 this means the set is empty.
if(mySet1.size()==0){
System.out.println("mySet1 is empty");
}
Check If Set Contains Given Element
Another important function checks if the given set contains the given element. We can use contains()
function by providing the element or object we want to check. This function will return true if the given element or object exists in the given set and return false if not.
if(mySet1.contains("Ali"){
System.out.println("mySet1 contains 'Ali'");
}
Iterate Over Set
In Java programming language Set base on Collections. Collections provide an array or list like features for example iteration. We can use iteration in Set too. we can use different loop statements like for, while, foreach, etc.
for(Iterator<E> iterator = mySet.iterator(); iterator.hasNext()) {
E element = iterator.next();
element.someMethod();
iterator.remove(element);
}