- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Check if a set is the subset of another set
In this article, we will understand how to check if a set is the subset of another set. 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 −
Is a sets sub-set of the other? true
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 - Create a Boolean variable and call the ‘containsAll’ method on one set with respect to the other. Step 7 - This checks if one set is a subset of the other. Step 8 - If yes, it returns True, else False. Step 9 - Display the result on the console. Step 10 - 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); boolean result = input_set_1.containsAll(input_set_2); System.out.println("\nIs a sets sub-set of the other? \n" + result); } }
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] Is a sets sub-set of the other? true
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 is_subset(Set<Integer> input_set_1, Set<Integer> input_set_2){ boolean result = input_set_1.containsAll(input_set_2); System.out.println("\nIs a sets sub-set of the other? \n" + result); } 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); is_subset(input_set_1, 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] Is a sets sub-set of the other? true
- Related Articles
- Swift Program to Check if the given Set is the Subset of Another Set
- How to check if a set is a subset of another set in R?
- Swift Program to Check if the given Set is the Superset of Another Set
- Swift Program to Check if the given Set is the Disjoint of Another Set
- How to check if a string is a subset of another string in R?
- Java Program to check if the String contains any character in the given set of characters
- What is Universal set and Subset ?
- How to check if a timestamp is set in MySQL?
- How to check if a cookie is set in Laravel?
- Golang program to check if k’th bit is set for a given number or not.
- Python program to check if both halves of the string have same set of characters.
- Program to convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Java Program to check whether one String is a rotation of another.
- Java Program to set a spinner of dates in Java

Advertisements