Java Check Array contains a Value using Apache Commons Lang

Tags: ArrayUtils Apache Commons Apache Commons Lang array

In this Java tutorial we learn how to check if an array contains a given value using the ArrayUtils class of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to check an array contains a value in Java

The Apache Commons Lang library provides the method ArrayUtils.contains() to check if a value is in a given array.

The below example to code shows you how to use ArrayUtils.contains() method with an int array.

CheckArrayContainsInt.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsInt {
    public static void main(String... args) {
        int[] arrayToCheck = new int[] { 123, 456, 789 };

        boolean result1 = ArrayUtils.contains(arrayToCheck, 456);
        boolean result2 = ArrayUtils.contains(arrayToCheck, 900);

        System.out.println("arrayToCheck contains '456' value: " + result1);
        System.out.println("arrayToCheck contains '900' value: " + result2);
    }
}
The output is:
arrayToCheck contains '456' value: true
arrayToCheck contains '900' value: false

The below example to code shows you how to use the ArrayUtils.contains() method with a long array.

CheckArrayContainsLong.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsLong {
    public static void main(String... args) {
        long[] arrayToCheck = new long[] { 123456, 4567789 };

        boolean result1 = ArrayUtils.contains(arrayToCheck, 123456);
        boolean result2 = ArrayUtils.contains(arrayToCheck, 999999);

        System.out.println("arrayToCheck contains '123456' value: " + result1);
        System.out.println("arrayToCheck contains '999999' value: " + result2);
    }
}
The output is:
arrayToCheck contains '123456' value: true
arrayToCheck contains '999999' value: false

The below example to code shows you how to use ArrayUtils.contains() method with a double array.

CheckArrayContainsDouble.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsDouble {
    public static void main(String... args) {
        double[] arrayToCheck = new double[] { 100.1, 200.2, 300.3 };

        boolean result1 = ArrayUtils.contains(arrayToCheck, 200.20);
        boolean result2 = ArrayUtils.contains(arrayToCheck, 400.4);

        System.out.println("arrayToCheck contains '200.20' value: " + result1);
        System.out.println("arrayToCheck contains '400.4' value: " + result2);
    }
}
The output is:
arrayToCheck contains '200.20' value: true
arrayToCheck contains '400.4' value: false

The below example to code shows you how to use ArrayUtils.contains() method with a float array.

CheckArrayContainsFloat.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsFloat {
    public static void main(String... args) {
        float[] arrayToCheck = new float[] { 10.1f, 20.2f, 30.3f };

        boolean result1 = ArrayUtils.contains(arrayToCheck, 20.20f);
        boolean result2 = ArrayUtils.contains(arrayToCheck, 40.4f);

        System.out.println("arrayToCheck contains '20.20f' value: " + result1);
        System.out.println("arrayToCheck contains '40.4f' value: " + result2);
    }
}
The output is:
arrayToCheck contains '20.20f' value: true
arrayToCheck contains '40.4f' value: false

The below example to code shows you how to use ArrayUtils.contains() method with a boolean array.

CheckArrayContainsBoolean.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsBoolean {
    public static void main(String... args) {
        boolean[] arrayToCheck = new boolean[] {false};

        boolean result1 = ArrayUtils.contains(arrayToCheck, true);
        boolean result2 = ArrayUtils.contains(arrayToCheck, false);

        System.out.println("arrayToCheck contains 'true' value: " + result1);
        System.out.println("arrayToCheck contains 'false' value: " + result2);
    }
}
The output is:
arrayToCheck contains 'true' value: false
arrayToCheck contains 'false' value: true

The below example to code shows you how to use ArrayUtils.contains() method with a byte array.

CheckArrayContainsByte.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsByte {
    public static void main(String... args) {
        byte[] arrayToCheck = new byte[] { 1, 2, 3, 4, 5, 6};

        boolean result1 = ArrayUtils.contains(arrayToCheck, (byte)3);
        boolean result2 = ArrayUtils.contains(arrayToCheck, (byte)7);

        System.out.println("arrayToCheck contains '3' value: " + result1);
        System.out.println("arrayToCheck contains '7' value: " + result2);
    }
}
The output is:
arrayToCheck contains '3' value: true
arrayToCheck contains '7' value: false

The below example to code shows you how to use ArrayUtils.contains() method with a char array.

CheckArrayContainsChar.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsChar {
    public static void main(String... args) {
        char[] arrayToCheck = new char[] { 'a', 'b', 'c', 'd'};

        boolean result1 = ArrayUtils.contains(arrayToCheck, 'c');
        boolean result2 = ArrayUtils.contains(arrayToCheck, 'e');

        System.out.println("arrayToCheck contains 'c' value: " + result1);
        System.out.println("arrayToCheck contains 'e' value: " + result2);
    }
}
The output is:
arrayToCheck contains 'c' value: true
arrayToCheck contains 'e' value: false

The below example to code shows you how to use ArrayUtils.contains() method with a short array.

CheckArrayContainsShort.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsShort {
    public static void main(String... args) {
        short[] arrayToCheck = new short[] {  8, 9, 10 };

        boolean result1 = ArrayUtils.contains(arrayToCheck, (short)9);
        boolean result2 = ArrayUtils.contains(arrayToCheck, (short)11);

        System.out.println("arrayToCheck contains '9' value: " + result1);
        System.out.println("arrayToCheck contains '11' value: " + result2);
    }
}
The output is:
arrayToCheck contains '9' value: true
arrayToCheck contains '11' value: false

You can also use the ArrayUtils.contains() method with an array of objects, for example array of String as the below example.

CheckArrayContainsString.java

import org.apache.commons.lang3.ArrayUtils;

public class CheckArrayContainsString {
    public static void main(String... args) {
        String[] arrayToCheck = new String[] { "Simple", "Solution", "Java" };

        boolean result1 = ArrayUtils.contains(arrayToCheck, "Simple");
        boolean result2 = ArrayUtils.contains(arrayToCheck, "Spring");

        System.out.println("arrayToCheck contains 'Simple' value: " + result1);
        System.out.println("arrayToCheck contains 'Spring' value: " + result2);
    }
}
The output is:
arrayToCheck contains 'Simple' value: true
arrayToCheck contains 'Spring' value: false

Happy Coding 😊