Example of Java containsAll() method
Java containsAll() method can be implemented with any collection some of them are mentioned with example bellow.
Example 1:(Using List)
Below is the implementation of the above method:
Java
// Java Program demomstrate // Java containsAll() method import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; // Driver Class class GFG { // main function public static void main(String[] args) { // define an ArrayList to store [1,2,3,4,5] List<Integer> list1 = new ArrayList<Integer>( Arrays.asList( 1 , 2 , 3 , 4 , 5 )); // define an ArrayList to store [1,2,3] List<Integer> list2 = new ArrayList<Integer>( Arrays.asList( 1 , 2 , 3 )); // define an ArrayList to store [1,2,7] List<Integer> list3 = new ArrayList<Integer>( Arrays.asList( 1 , 2 , 7 )); // [1,2,3,4,5] System.out.println( "List1 elements: " + list1); // [1,2,3] System.out.println( "List2 elements: " + list2); // the below step prints "true" because elements // [1,2,3] are present in [1,2,3,4,5] System.out.println( "Is the List1 contains all the elements of List2 ? " + list1.containsAll(list2)); //[1,2,7] System.out.println( "List3 elements: " + list3); // the below step prints "false" because 7 is not // present in [1,2,3,4,5] System.out.println( "Is the List1 contains all the elements of List3 ? " + list1.containsAll(list3)); } } |
Output
List1 elements: [1, 2, 3, 4, 5] List2 elements: [1, 2, 3] Is the List1 contains all the elements of List2 ? true List3 elements: [1, 2, 7] Is the List1 contains all the elements of List3 ? false
Example 2:(Using Set)
Below is the implementation of the above method:
Java
// Java Program demomstrate // Java containsAll() method import java.io.*; import java.util.HashSet; import java.util.Set; // Driver Class class GFG { // main function public static void main(String[] args) { // define set1 to store the elements // stores ["apple","tomato", "carrot"] Set<String> set1 = new HashSet<String>(); set1.add( "carrot" ); set1.add( "apple" ); set1.add( "tomato" ); // define set2 to store the elements // stores ["apple", "banana", "carrot", "tomato"] Set<String> set2 = new HashSet<String>(); set2.add( "carrot" ); set2.add( "apple" ); set2.add( "tomato" ); set2.add( "apple" ); set2.add( "banana" ); // define set3 to store the elements // stores ["apple", "tomato", "carrot"] Set<String> set3 = new HashSet<String>(); set3.add( "tomato" ); set3.add( "carrot" ); set3.add( "apple" ); set3.add( "apple" ); // ["apple", "tomato", "carrot"] System.out.println( "unique Set1 elements: " + set1); // ["apple", "banana", "carrot", "tomato"] System.out.println( "unique Set2 elements: " + set2); // The below step prints "false" because // set1 donot contains the element "banana" System.out.println( "Is set1 contains all the elements of set2? " + set1.containsAll(set2)); // ["apple", "tomato", "carrot"] System.out.println( "unique Set3 elements: " + set3); // The below step prints "true" because set1 // contains all the elements of set3 System.out.println( "Is set1 contains all the elements of set3? " + set1.containsAll(set3)); } } |
Output
unique Set1 elements: [apple, tomato, carrot] unique Set2 elements: [banana, apple, tomato, carrot] Is set1 contains all the elements of set2? false unique Set3 elements: [apple, tomato, carrot] Is se...
Java Collection containsAll() Method with Examples
The Collection interface in Java is the fundamental part of the Collection hierarchy. It is found in the java.util package and offers a wide range of built-in classes and methods. One such useful method is containsAll().
Contact Us