Java Program to Calculate the Difference Between two Sets

Java Program to Calculate the difference between two sets

What is a Set in Java ?

In Java, a Set is a collection that cannot contain duplicate elements. It is an interface in the Java Collection Framework, and has several implementing classes such as HashSet, TreeSet, and LinkedHashSet.

A Set can be useful when you need to store a collection of unique items and perform operations such as testing for membership, adding or removing elements, or iterating through the elements.

What is Set Difference ?

In set theory, the difference between two sets is a new set containing all elements that are in the first set but not in the second. It is represented using the symbol “-” and is called the set difference. For example, if A = {1, 2, 3} and B = {2, 3, 4}, then A – B = {1}.

Ways to calculate the set difference :

  • In Java, you can use the removeAll() method of the Set interface to calculate the difference between two sets. Here’s an example:
Set <Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3));
Set <Ingeger> set2 = new HashSet<>(Arrays.asList(2, 3, 4));
set1.removeAll(set2);
System.out.println(set1); // Output: [1]

Note : In this example, the difference between set1 and set2 is calculated by removing all elements of set2 from set1. The resulting set, set1, now contains only the elements that were present in set1 but not in set2.

Example 1 : Calculation of  Difference using removeAll() Method :

Run

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set <Integer> set1 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);

        Set <Integer> set2 = new HashSet<>();
        set2.add(2);
        set2.add(3);
        set2.add(4);

        // Use the retainAll() method to find the difference
        set1.removeAll(set2);

        System.out.println("Difference between the sets: " + set1);
    }
}

Output :

Difference between the sets: [1]

Example 2 : 

Run

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set <String> set1 = new HashSet<>();
        set1.add("apple");
        set1.add("banana");
        set1.add("orange");

        Set <String> set2 = new HashSet<>();
        set2.add("banana");
        set2.add("orange");
        set2.add("mango");

        // Use the removeAll() method to find the difference
        set1.removeAll(set2);

        System.out.println("Difference between the sets: " + set1);
    }
}

Output :

Difference between the sets: [apple]

Exaplanation : 

In this example we have created two sets, set1 and set2, both of which are sets of strings.
set1 is initialized with the strings “apple”, “banana”, and “orange”, and set2 is initialized with the strings “banana”, “orange”, and “mango”.
Then the removeAll() method is used to remove all the elements present in set2 from set1.
After that, The difference between the two sets is stored in set1 and then it is printed. The output is Difference between the sets: [apple].
The removeAll() method compares each element of set1 with the elements of set2 and removes the elements which are present in both sets.
So, in this case, “apple” is only present in set1, it is not present in set2 so it will be in the final set1 after using removeAll() method

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription