Situatie
In this example, we will learn to check if a set is the subset of another set in Java.
Solutie
Pasi de urmat
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);
}
}
Output
Numbers: [1, 2, 3, 4]
Prime Numbers: [2, 3]
Is Prime Numbers is subset of Numbers? true
Leave A Comment?