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

In this example, we will learn to check if a set is the subset of another set in Java.

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

``````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<>();
System.out.println("Numbers: " + numbers);

// create the second set

// check if primeNumbers is a subset of numbers
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```

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

``````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<>();
System.out.println("Programming Languages: " + languages);

// create the second set
Set<String> frontend = new TreeSet<>();
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);
}
}``````

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.