Calculate the Difference Between Two Sets in Java



In this article, we will understand how to calculate the difference between two sets in Java . A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Problem Statement

Write a Java program to calculate the difference between two sets. Below is a demonstration of the same ?

Input

First set: [90, 75, 60, 45]
Second set: [90, 60]

Output

After subtraction of two sets:
[75, 45]

Using the main method

The following are the steps to calculate the difference between two sets using the main() method ?

  • Import the HashSet and Set class from java.util packages.
  • Initialize two sets using HashSet to store integer values.
  • Add elements to both sets using the add() method.
  • Print the sets to display their contents before subtraction.
  • Subtract the second set from the first using the removeAll() method.
  • Print the result to display the difference between the two sets.

Example

Here, we bind all the operations together under the ?main' method

import java.util.HashSet;
import java.util.Set;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      Set<Integer> input_set_1 = new HashSet<>();
      input_set_1.add(45);
      input_set_1.add(60);
      input_set_1.add(75);
      input_set_1.add(90);
      System.out.println("The first set is defined as: " + input_set_1);
      Set<Integer> input_set_2 = new HashSet<>();
      input_set_2.add(60);
      input_set_2.add(90);
      System.out.println("The second set is defined as: " + input_set_2);
      input_set_1.removeAll(input_set_2);
      System.out.println("\nAfter subtraction of two sets: \n" + input_set_1);
   }
}

Output

The required packages have been imported
The first set is defined as: [90, 75, 60, 45]
The second set is defined as: [90, 60]

After subtraction of two sets:
[75, 45]

Using encapsulation

Following are the steps to calculate the difference between two sets using the encapsulation ?

  • Import the necessary packages.
  • Define a static method subtract() that takes two sets as parameters and removes all elements of the second set from the first.
  • Initialize two sets using HashSet and add elements using the add() method.
  • Print the sets to display their contents before subtraction.
  • Call the subtract() method to calculate and print the difference between the two sets.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming

import java.util.HashSet;
import java.util.Set;
public class Demo {
   static void subtract(Set<Integer> input_set_1, Set<Integer> input_set_2){
      input_set_1.removeAll(input_set_2);
      System.out.println("\nAfter subtraction of two sets: \n" + input_set_1);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      Set<Integer> input_set_1 = new HashSet<>();
      input_set_1.add(45);
      input_set_1.add(60);
      input_set_1.add(75);
      input_set_1.add(90);
      System.out.println("The first set is defined as: " + input_set_1);
      Set<Integer> input_set_2 = new HashSet<>();
      input_set_2.add(60);
      input_set_2.add(90);
      System.out.println("The second set is defined as: " + input_set_2);
      subtract(input_set_1, input_set_2);
   }
}

Output

The required packages have been imported
The first set is defined as: [90, 75, 60, 45]
The second set is defined as: [90, 60]

After subtraction of two sets:
[75, 45]
Updated on: 2024-09-09T01:18:44+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements