Set containsAll() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface. Example 1: This example checks if all elements of one set are present in another set and it will return true if they are identical. Java // Java program to demonstrates // the working of containsAll() method import java.util.*; class Geeks { public static void main(String args[]) { // Creating an empty set Set<String> s1 = new HashSet<String>(); // Use add() method to // add elements in the set s1.add("Java"); s1.add("C++"); s1.add("Python"); System.out.println("Set 1: " + s1); // Creating another empty set Set<String> s2 = new HashSet<String>(); // Use add() method to // add elements in the set s2.add("Java"); s2.add("C++"); s2.add("Python"); System.out.println("Set 2: " + s2); // Check if the set // contains same elements System.out.println("Does set 1 contains set 2?: " + s1.containsAll(s2)); } } OutputSet 1: [Java, C++, Python] Set 2: [Java, C++, Python] Does set 1 contains set 2?: true Syntax of containsAll() Methodboolean containsAll(Collection<?> c)Parameter: "c" is the collection whose elements are to be checked in the current collection.Return Type: This method returns "true" if the current collection contains all the elements of the specified collection, otherwise returns "false".Example 2: This example checks if all elements of one set are present in another set and it will return false if they are not identical. Java // Java program to demonstrates that // containsALl() method returns false import java.util.*; class Geeks { public static void main(String args[]) { // Creating an empty s1 Set<String> s1 = new HashSet<String>(); // Use add() method to add elements in s1 s1.add("Java"); s1.add("C++"); s1.add("Python"); System.out.println("Set 1: " + s1); // Creating another empty s2 Set<String> s2 = new HashSet<String>(); // Use add() method to add elements in s2 s2.add("10"); s2.add("20"); System.out.println("Set 2: " + s2); // Check if s1 contains all elements of s2 System.out.println("Does s1 contain all elements of s2: " + s1.containsAll(s2)); } } OutputSet 1: [Java, C++, Python] Set 2: [20, 10] Does s1 contain all elements of s2: false Comment More info G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like