
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Calculate the difference between two sets
In this article, we will understand how to calculate the difference between two sets. 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.
Below is a demonstration of the same −
Suppose our input is −
First set: [90, 75, 60, 45] Second set: [90, 60]
The desired output would be −
After subtraction of two sets: [75, 45]
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create two Sets, and add elements to it using the ‘add’ method. Step 5 - Display the Sets on the console. Step 6 - Compute the difference of the sets using the ‘removeAll’ method. Step 7 - Display the difference of sets on the console. Step 8 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
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]
Example 2
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]
- Related Articles
- Java Program to Calculate the intersection of two sets
- Java Program to Calculate union of two sets
- Java Program to compare two sets
- Golang program to calculate difference between two slices
- C Program to calculate the difference between two time periods
- Golang program to calculate the symmetric difference between two slices
- C++ Program to Calculate Difference Between Two Time Period
- Golang Program to Calculate Difference Between Two Time Periods
- Get the asymmetric difference of two sets in Java
- How to calculate the difference between two dates in JavaScript?
- Java program to calculate the product of two numbers
- Java Program to get the difference between two time zones by seconds
- C program to calculate distance between two points
- Swift Program to Calculate Distance Between Two Points
- Merge two sets in Java

Advertisements