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 a 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 not allowed.

Following are the ways to check if a set is a subset of another set:

Using containsAll() Method

In Java, the set interface has a method called containsAll() that checks if a set contains all elements of another set. We can use this method to check if one set is a subset of another set.

Syntax

Following is the syntax of the containsAll() method. The first argument is the set to be checked, and the second argument is the set to be checked against.

set1.containsAll(set2);

Example

In the following example, we will create two sets and then check if one set is a subset of another set using the containsAll() method.

import java.util.HashSet;
import java.util.Set;
public class SubsetCheck {
   public static void main(String[] args) {
      Set<Integer> set1 = new HashSet<>();
      set1.add(1);
      set1.add(2);
      set1.add(3);
      
      Set<Integer> set2 = new HashSet<>();
      set2.add(2);
      set2.add(3);
      
      System.out.println("Set 1: " + set1);
      System.out.println("Set 2: " + set2);
      
      boolean isSubset = set1.containsAll(set2);
      
      if (isSubset) {
         System.out.println("Set 2 is a subset of Set 1");
      } else {
         System.out.println("Set 2 is not a subset of Set 1");
      }
   }
}

Following is the output of the above code:

Set 1: [1, 2, 3]
Set 2: [2, 3]
Set 2 is a subset of Set 1

Using Stream API

In the Java Stream API, we can use the allMatch() method to check if all elements of one set are present in another set. This method returns true if all elements of the stream match the given predicate.

Syntax

Following is the syntax of the allMatch() method. The first argument is the set to be checked, and the second argument is the set to be checked against.

set1.stream().allMatch(set2::contains);

Example

In the following example, we will create two sets and then check if one set is a subset of another set using the Stream API.

import java.util.HashSet;
import java.util.Set;
public class SubsetCheck {
   public static void main(String[] args) {
      Set<Integer> set1 = new HashSet<>();
      set1.add(1);
      set1.add(2);
      set1.add(3);
      
      Set<Integer> set2 = new HashSet<>();
      set2.add(2);
      set2.add(3);
      
      System.out.println("Set 1: " + set1);
      System.out.println("Set 2: " + set2);
      
      boolean isSubset = set2.stream().allMatch(set1::contains);
      
      if (isSubset) {
         System.out.println("Set 2 is a subset of Set 1");
      } else {
         System.out.println("Set 2 is not a subset of Set 1");
      }
   }
}

Following is the output of the above code:

Set 1: [1, 2, 3]
Set 2: [2, 3]
Set 2 is a subset of Set 1
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-18T16:27:32+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements