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

Java Program to check subset of a set

What are Sets?

In Java, the Set interface is a member of the Java Collections Framework and extends the Collection interface. It is an unordered collection of objects, in which duplicate values cannot be stored.

In this Article, we will write a program to check if a set is the subset of another set.

Sets :

In Java, a Set is a collection that cannot contain duplicate elements. It is an interface that extends the Collection interface and is a member of the Java Collections Framework.

There are several implementations of the Set interface in the Java Collections Framework, including:

  • HashSet : This implementation uses a hash table to store the elements. It offers constant-time performance for the basic operations (add, remove, contains, and size), but it does not maintain the insertion order of the elements.
  • TreeSet : This implementation stores the elements in a sorted tree structure. It offers logarithmic time performance for the basic operations (add, remove, and contains), and it maintains the natural ordering of the elements (or allows you to specify a custom Comparator ).
  • LinkedHashSet : This implementation is similar to HashSet, but it maintains a doubly-linked list of the entries in the set, which allows it to preserve the insertion order of the elements.

Here is an example of how you might use a Set in Java:

Run

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

public class Main {
  public static void main(String[] args) {
    Set < String > set = new HashSet<>();

    // Adding elements to the the hashset
    set.add("Lakshit");
    set.add("Mittal");
    set.add("PrepInsta");

    // Printing the Hashset
    for (String s : set) {
      System.out.println(s);
    }
  }
}

Output:

Mittal
PrepInsta
Lakshit

Java program to check if a Set is a subset of another Set:

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

public class Main {
  public static void main(String[] args) {
      
    // Adding Elements in set 1
    Set set1 = new HashSet<>();
    set1.add(1);
    set1.add(2);
    set1.add(3);

    // Adding Elements in set 2
    Set set2 = new HashSet<>();
    set2.add(2);
    set2.add(3);
    set2.add(4);
    
    // Check if a set is the subset of another set using containsAll function
    boolean isSubset = set1.containsAll(set2);
    
    // Printing true or false if a set is the subset of another
    System.out.println(isSubset);
  }
}
Output:

false

In this example, we create two sets: set1 and set2. set1 contains the elements 1, 2, and 3, and set2 contains the elements 2, 3, and 4. We then use the containsAll method to check if all of the elements in set2 are contained in set1. Since set1 does not contain the element 4, isSubset is assigned the value false.

Note that the containsAll method returns true if and only if the specified collection contains all of the elements in this set. If the specified collection is empty, this method returns true.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription