In this post under Apache Collections, I will show with example the purpose of “CollectionUtils.containsAll” method.
While developing applications we come around situations where we have to check whether elements of one collection is also present in another collection also or not.
To do this, Apache Collections framework provides “CollectionUtils” utility class “containsAll” method.
This method, checks whether all the elements of one collection is also present in another collection also or not.
If all elements of a collection is present in another collection it returns true or else false.
The method declaration according to Javadoc is as shown below
public static boolean containsAll(Collection<?> coll1, Collection<?> coll2)
The method compares the elements of “coll2” with the elements of “coll1”. If all “coll2” elements are present in “coll1”, it returns true or else false.
If the collection “coll2” is empty, the result will be true.
Below is main code showing how to use it.
Main class
1 package defaultPackage;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.commons.collections4.CollectionUtils;
7
8 public class Example4 {
9 public static void main(String[] args) {
10 List<Integer> list1 = new ArrayList<Integer>();
11 List<Integer> list2 = new ArrayList<Integer>();
12 List<Integer> list3 = new ArrayList<Integer>();
13 List<Integer> list4 = new ArrayList<Integer>();
14 List<Integer> list5 = new ArrayList<Integer>();
15
16 list1.add(1);
17 list1.add(2);
18 list1.add(3);
19 list1.add(4);
20 list1.add(5);
21
22 list2.add(6);
23 list2.add(7);
24 list2.add(8);
25 list2.add(9);
26 list2.add(10);
27
28 list3.add(1);
29 list3.add(2);
30 list3.add(3);
31 list3.add(4);
32 list3.add(5);
33
34 list4.add(1);
35 list4.add(2);
36 list4.add(3);
37 list4.add(6);
38 list4.add(7);
39
40 list5.add(1);
41 list5.add(2);
42 list5.add(3);
43
44 boolean result = CollectionUtils.containsAll(list1, list2);
45 System.out.println("list1 vs list2 Comparison result: " + result);
46
47 result = CollectionUtils.containsAll(list1, list3);
48 System.out.println("list1 vs list3 Comparison result: " + result);
49
50 result = CollectionUtils.containsAll(list1, list4);
51 System.out.println("list1 vs list4 Comparison result: " + result);
52
53 result = CollectionUtils.containsAll(list1, list5);
54 System.out.println("list1 vs list5 Comparison result: " + result);
55 }
56 }
In the above code, I have created 5 lists “list1”, “list2”, “list3”, “list4”, and “list5”.
We have added elements to those lists.
At line 44, we compare “list1” and “list2” using “containsAll” method. Since both lists have different elements, the result will be false.
At line 47, we compare “list1” and “list3” using “containsAll” method. Since both lists have same elements, the result will be true.
At line 50, we compare “list1” and “list4” using “containsAll” method. Since “list4” has some elements which are matching and not all, the result will be false.
At line 53, we compare “list1” and “list5” using “containsAll” method. Since “list5” all elements are present in “list1” the result will be true.