Write a Java Program to Check if a set is the subset of another set

Java Program to Check if a set is the subset of another set

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Check if a set is the subset of another set
Java Program to Check if a set is the subset of another set

Write a Java Program to Check if a set is the subset of another set

PROGRAM: Java Program to Check if a set is the subset of another set

/*Java Program to Check if a set is the subset of another set*/

1: Check subset of a Set using HashSet class

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

class Main {
public static void main(String[] args) {

// create the first set
Set<Integer> numbers = new HashSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
System.out.println(“Numbers: ” + numbers);

// create the second set
Set<Integer> primeNumbers = new HashSet<>();
primeNumbers.add(2);
primeNumbers.add(3);
System.out.println(“Prime Numbers: ” + primeNumbers);

// check if primeNumbers is a subset of numbers
boolean result = numbers.containsAll(primeNumbers);
System.out.println(“Is Prime Numbers is subset of Numbers? ” + result);
}
}

2: Check subset of a Set using TreeSet class

import java.util.TreeSet;
import java.util.Set;

class Main {
public static void main(String[] args) {

// create the first set
Set<String> languages = new TreeSet<>();
languages.add(“Java”);
languages.add(“JavaScript”);
languages.add(“Python”);
languages.add(“CSS”);
System.out.println(“Programming Languages: ” + languages);

// create the second set
Set<String> frontend = new TreeSet<>();
frontend.add(“CSS”);
frontend.add(“JavaScript”);
System.out.println(“Frontend Languages: ” + frontend);

// check if frontend is a subset of languages
boolean result = languages.containsAll(frontend);
System.out.println(“Is frontend is subset of languages? ” + result);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *