
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to calculate the difference between two sets
A Set is a Collection that cannot contain duplicate elements (same as the mathematical set abstraction). The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Finding the difference between two sets means finding the elements that are not similar in both sets. For example, we have set A = {1, 2, 3} and set B = {2, 3, 4}. The difference between these two sets will be {1, 4} because 1 is not in set B and 4 is not in set A.
In this article, we will learn how to calculate the difference between two sets in Java. The following are the ways to calculate the difference between two sets:
Using forEach() Method
The forEach() method of the Set Interface (inherited from the Iterable interface) accepts a Consumer object as a parameter. The Consumer is a functional interface that represents an operation that takes a single parameter and returns nothing.
To calculate the difference between two sets, we need to create a consumer object that iterates over a set and checks if each element is present in the other. If an element is not present in the second set, we can add it to a new set that will contain the difference.
We can also use the contains() method and check if an element is present in the set, and add the elements to the set using the add() method.
Example
In the following example, we will create two sets and then calculate the difference between them using the forEach() method.
import java.util.HashSet; import java.util.Set; public class SetDiff { public static void main(String[] args){ Set<Integer> setA = new HashSet<>(); setA.add(1); setA.add(2); setA.add(3); Set<Integer> setB = new HashSet<>(); setB.add(2); setB.add(3); setB.add(4); System.out.println("Set A: " + setA); System.out.println("Set B: " + setB); Set<Integer> difference = new HashSet<>(); setA.forEach(el -> { if(!setB.contains(el)){ difference.add(el); } }); System.out.println("Difference :" + difference); } }
Following is the output of the above code:
Set A: [1, 2, 3] Set B: [2, 3, 4] Difference :[1, 4]
Using Stream API
In the Java Stream API, we can use the filter() method to filter those elements of the first set that are not present in the second set.
We will use the collect() method to collect the filtered elements into a new set that will contain the difference. The Collectors.toSet() method is used to collect the elements into a set.
Example
In the following example, we will create two sets and then calculate the difference between them using the Stream API.
import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; public class SetDiff { public static void main(String[] args){ Set<Integer> setA = new HashSet<>(); setA.add(1); setA.add(2); setA.add(3); Set<Integer> setB = new HashSet<>(); setB.add(2); setB.add(3); setB.add(4); System.out.println("Set A: " + setA); System.out.println("Set B: " + setB); Set<Integer> difference = setA.stream() .filter(el -> !setB.contains(el)) .collect(Collectors.toSet()); System.out.println("Difference :" + difference); } }
Following is the output of the above code:
Set A: [1, 2, 3] Set B: [2, 3, 4] Difference :[1, 4]
Using removeAll() Method
The removeAll() method is used for removing all elements of another collection from the set. It accepts a Collection object as an argument and removes all the elements of that collection from the set.
Example
In the following example, we will add elements to two sets using the add() method. Then subtract the second set from the first using the removeAll() method. After removing the elements, the first set will contain only those elements that are not present in the second set.
import java.util.HashSet; import java.util.Set; public class SetDiff { public static void main(String[] args){ Set<Integer> setA = new HashSet<>(); setA.add(1); setA.add(2); setA.add(3); Set<Integer> setB = new HashSet<>(); setB.add(2); setB.add(3); setB.add(4); System.out.println("Set A: " + setA); System.out.println("Set B: " + setB); Set<Integer> difference = new HashSet(setA); difference.removeAll(setB); System.out.println("Difference :" + difference); } }
Following is the output of the above code:
Set A: [1, 2, 3] Set B: [2, 3, 4] Difference :[1]