Wednesday, July 3, 2024
HomeLanguagesJavaTreeSet containsAll() method in Java with Example

TreeSet containsAll() method in Java with Example

The containsAll() method of Java TreeSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set.

Syntax:

public boolean containsAll(Collection C)

Parameters: The parameter C is a Collection. This parameter refers to the set whose elements occurrence is needed to be checked in this set.

Return Value: The method returns True if this set contains all the elements of other set otherwise it returns False.

Below programs illustrate the TreeSet.containsAll() method:

Program 1:




// Java code to illustrate
// TreeSet containsAll()
  
import java.util.*;
  
class TreeSetDemo {
    public static void main(String args[])
    {
  
        // Creating an empty set
        TreeSet<String>
            set = new TreeSet<String>();
  
        // Use add() method to
        // add elements in the set
        set.add("Geeks");
        set.add("for");
        set.add("Geeks");
        set.add("10");
        set.add("20");
  
        // prints the set
        System.out.println("TreeSet 1: "
                           + set);
  
        // Creating another empty set
        TreeSet<String>
            set2 = new TreeSet<String>();
  
        // Use add() method to
        // add elements in the set
        set2.add("Geeks");
        set2.add("for");
        set2.add("Geeks");
        set2.add("10");
        set2.add("20");
  
        // prints the set
        System.out.println("TreeSet 2: "
                           + set2);
  
        // Check if the set
        // contains same elements
        System.out.println("\nDoes set 1 contains set 2: "
                           + set.containsAll(set2));
    }
}


Output:

TreeSet 1: [10, 20, Geeks, for]
TreeSet 2: [10, 20, Geeks, for]

Does set 1 contains set 2: true

Program 2:




// Java code to illustrate boolean containsAll()
  
import java.util.*;
  
class TreeSetDemo {
    public static void main(String args[])
    {
  
        // Creating an empty set
        TreeSet<String>
            set = new TreeSet<String>();
  
        // Use add() method to
        // add elements in the set
        set.add("Geeks");
        set.add("for");
        set.add("Geeks");
  
        // prints the set
        System.out.println("TreeSet 1: "
                           + set);
  
        // Creating another empty set
        TreeSet<String>
            set2 = new TreeSet<String>();
  
        // Use add() method to
        // add elements in the set
        set2.add("10");
        set2.add("20");
  
        // prints the set
        System.out.println("TreeSet 2: "
                           + set2);
  
        // Check if the set
        // contains same elements
        System.out.println("\nDoes set 1 contains set 2: "
                           + set.containsAll(set2));
    }
}


Output:

TreeSet 1: [Geeks, for]
TreeSet 2: [10, 20]

Does set 1 contains set 2: false

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments