The containsAll()
method in the CollectionUtils class from Apache Commons Collections library in Java is used to check if a collection contains all elements in another collection. It returns true if the target collection contains all elements of the specified collection, and false otherwise. In this post, we will see CollectionUtils containsAll() example in Java. we will see how to use CollectionUtils containsAll
() method.
We need to add the below dependency in maven to use org.apache.commons.lang3.CollectionUtils containsAll()
method. We can download the apache-commons maven dependency from here.
pom.xml changes
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
CollectionUtils containsAll() Example in Java
Example 1
package com.javatute;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionUtilsContainsAllExample {
public void getConsumerInfo() {
}
public static void main(String[] args) {
List<String> ageList1 = new ArrayList<>();
ageList1.add("21");
ageList1.add("27");
ageList1.add("30");
ArrayList<String> ageList2 = new ArrayList<>();
ageList2.add("21");
ageList2.add("27");
ageList2.add("30");
boolean b = CollectionUtils.containsAll(ageList1, ageList2);
System.out.println(b);
}
}
Output is
true
Example 2
package com.javatute;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionUtilsContainsAllExample {
public void getConsumerInfo() {
}
public static void main(String[] args) {
List<String> marksList1 = new ArrayList<>();
marksList1.add("21");
marksList1.add("27");
marksList1.add("30");
ArrayList<String> marksList2 = new ArrayList<>();
marksList2.add("21");
marksList2.add("27");
marksList2.add("20");
boolean b = CollectionUtils.containsAll(marksList2, marksList1);
System.out.println(b);
}
}
Output is
false
Example 3
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
public class CollectionUtilsExample {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("apple");
list1.add("banana");
list1.add("cherry");
List<String> list2 = new ArrayList<>();
list2.add("banana");
list2.add("cherry");
boolean result = CollectionUtils.containsAll(list1, list2);
System.out.println("List 1 contains all elements of List 2: " + result);
}
}
Output – true
This example creates two lists of strings, list1
and list2
, and uses the containsAll()
method to check if all elements in list2
are contained in list1
. The method returns a true value.
That’s all about CollectionUtils containsAll() example in Java.
Related post.
- CollectionUtils permutations() Example in Java
- CollectionUtils retainAll() Example in Java
- CollectionUtils reverseArray() Example in Java
- CollectionUtils select() Example in Java
- CollectionUtils removeAll() Example in Java
Summary – The CollectionUtils.containsAll()
method is a utility method from the Apache Commons Collections library. It checks if the elements of one collection (list2
in this example) are contained in another collection (list1
in this example). The method returns a boolean value of true
if all elements are found, and false
otherwise.