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

To understand this example, you should have the knowledge of the following Java programming topics:


Example 1: Check subset of a Set using HashSet class

Output

Numbers: [1, 2, 3, 4]
Prime Numbers: [2, 3]
Is Prime Numbers is subset of Numbers? true

In the above example, we have created two sets named numbers and primeNumbers. We have implemented the set using the HashSet class. Notice the line,

numbers.containsAll(primeNumbers);

Here, we have used the containsAll() method to check if primeNumbers is the subset of numbers.


Example 2: Check subset of a Set using TreeSet class

Output

Programming Languages: [CSS, Java, JavaScript, Python]
Frontend Languages: [CSS, JavaScript]
Is frontend is subset of languages? true

Here, we have implemented the set using the TreeSet class.