CollectionUtils containsAny() Example in Java

The containsAny() method in Java’s CollectionUtils class is used to check if a given collection contains any elements from another collection. In this post, we will see CollectionUtils containsAny() example in Java. We will see how to use CollectionUtils containsAny() method.

We need to add the below dependency in maven to use org.apache.commons.collections4.CollectionUtils.containsAny() 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 containsAny() Example in Java

Example 1

package com.javatute;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;

public class CollectionUtilsContainsAnyExample {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("1");
        list1.add("2");

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("1");
        list2.add("2");
        list2.add("3");

        boolean b = CollectionUtils.containsAny(list1, list2);
        System.out.println(b);
    }
}

Output is

true

Example 2

package com.javatute;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;

public class CollectionUtilsContainsAnyExample {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("11");
        list1.add("12");
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("112");
        list2.add("210");
        list2.add("143");
        boolean b = CollectionUtils.containsAny(list1, list2);
        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 CollectionUtilsContainAnyExample {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        list1.add("A");
        list1.add("B");
        list1.add("C");
        list1.add("D");

        List<String> list2 = new ArrayList<>();
        list2.add("C");
        list2.add("D");
        list2.add("E");
        list2.add("F");

        boolean containsAny = CollectionUtils.containsAny(list1, list2);
        System.out.println("List 1 contains any elements from List 2: " + containsAny);
    }
}

Output – List 1 contains any elements from List 2: true

Example 4

import org.apache.commons.collections4.CollectionUtils;
import java.util.Arrays;
import java.util.List;

public class CollectionUtilsExample {
  public static void main(String[] args) {
    List<String> list1 = Arrays.asList("A", "B", "C");
    List<String> list2 = Arrays.asList("D", "E", "F");
    
    boolean containsAny = CollectionUtils.containsAny(list1, list2);
    System.out.println("list1 contains any element from list2: " + containsAny);
  }
}

Output – list1 contains any element from list2: false

This code creates two lists list1 and list2, and then uses the containsAny() method to check if list1 contains any elements that are also in list2. The result is then printed to the console.

See docs.

That’s all about CollectionUtils containsAny() example in Java.

Related post.