CollectionUtils intersection method

In this post under Apache Collections, I will explain with example the purpose of CollectionUtils “intersection” method.

The “intersection” method compares two collections and return a new list containing items that are common in both of them.

Below is the complete main code for your reference.

Main class

1  package defaultPackage;
2  
3  import org.apache.commons.collections4.CollectionUtils;
4  
5  import java.util.ArrayList;
6  import java.util.List;
7  
8  public class Example10 {
9      public static void main(String[] args) {
10         List<Integer> intList1 = new ArrayList<>(0);
11         List<Integer> intList2 = new ArrayList<>(0);
12         List<Integer> intList3 = new ArrayList<>(0);
13 
14         intList1.add(1);
15         intList1.add(2);
16         intList1.add(3);
17         intList1.add(4);
18 
19         intList2.add(4);
20         intList2.add(5);
21         intList2.add(5);
22         intList2.add(7);
23 
24         intList3.add(5);
25         intList3.add(5);
26         intList3.add(5);
27         intList3.add(6);
28 
29         List<Integer> list = (List<Integer>)CollectionUtils.intersection(intList1, intList2);
30 
31         for(int i : list) {
32             System.out.println(i);
33         }
34         System.out.println("-----------------------------");
35         list = (List<Integer>)CollectionUtils.intersection(intList1, intList3);
36 
37         for(int i : list) {
38             System.out.println(i);
39         }
40         System.out.println("-----------------------------");
41         list = (List<Integer>)CollectionUtils.intersection(intList2, intList3);
42 
43         for(int i : list) {
44             System.out.println(i);
45         }
46     }
47 }

In the above code, I have created 3 list that will contain multiple values of type Integer “intList1”, “intList2”, and “intList3”.

Then from line 14 to 27, I populated all three list.

When we compare “intList1” with “intList2” by using “intersection” method at line 29, the common element is 4. So this will be printed in the console.

When we compare “intList1” with “intList3” by using “intersection” method at line 35, there is no common element. So an empty list will be created and nothing will be printed to console.

When we compare “intList2” with “intList3” by using “intersection” method at line 41, the common element is 5 and instead of printing once, it is printed twice. This happens because when same element is present in both the collection more than once, then the element is printed “n” times. Where “n” is the minimum out of frequency of the element in the left collection and frequency of element in the right collection is printed.

In other words if “f” is a frequency function which returns how many times a element is repeated in a collection. Then we have two collections “collection1” and “collection2” and “e” is the element which is present in both the collection. At that time the “intersection” method will print “n” times where

n = min(f(e in collection1), f(e in collection2)).

In this way we can use CollectionUtils “intersection” method

Below is the output

Output

4
-----------------------------
-----------------------------
5
5

Leave a comment