How do I find specific elements or object in an array?

This example demonstrate how to find specific items in array. We will use the org.apache.commons.lang.ArrayUtils class. This class provides method called contains(Object[] array, Object objectToFind) method to check if an array contains the objectToFind in it.

We can also use the indexOf(Object[] array, Object objectToFind) method and the lastIndexOf(Object[] array, Object objectToFind) method to get the index of an array element where our objectToFind is located.

package org.kodejava.example.commons.lang;

import org.apache.commons.lang.ArrayUtils;

public class ArrayUtilsIndexOfDemo {
    public static void main(String[] args) {
        String[] colours = { "Red", "Orange", "Yellow", "Green",
            "Blue", "Violet", "Orange", "Blue" };

        // Does colours array contains the Blue colour?
        boolean contains = ArrayUtils.contains(colours, "Blue");
        System.out.println("Contains Blue? " + contains);

        // Can you tell me the index of each colour defined bellow?
        int indexOfYellow = ArrayUtils.indexOf(colours, "Yellow");
        System.out.println("indexOfYellow = " + indexOfYellow);

        int indexOfOrange = ArrayUtils.indexOf(colours, "Orange");
        System.out.println("indexOfOrange = " + indexOfOrange);

        int lastIndexOfOrange = ArrayUtils.lastIndexOf(colours, "Orange");
        System.out.println("lastIndexOfOrange = " + lastIndexOfOrange);
    }
}

Here are the result of the code above.

Contains Blue? true
indexOfYellow = 2
indexOfOrange = 1
lastIndexOfOrange = 6

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar -->
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

How do I convert array of object to array of primitive?

In the code example below we demonstrate the ArrayUtils.toPrimitive() method to convert an array of Integer object to an array of its primitive type. Besides converting array of Integer objects this method is overloaded to accept other types of object array.

package org.kodejava.example.commons.lang;

import org.apache.commons.lang.ArrayUtils;

public class ArrayObjectToPrimitiveDemo {
    public static void main(String[] args) {
        // An array of Integer objects.
        Integer[] integers = {1, 2, 3, 5, 8, 13, 21, 34, 55};
        Boolean[] booleans = {Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE};

        // Convert array of Integer objects into array of type int.
        int[] ints = ArrayUtils.toPrimitive(integers);
        System.out.println(ArrayUtils.toString(ints));

        // Convert array of Boolean objects into array of type boolean.
        boolean[] bools = ArrayUtils.toPrimitive(booleans);
        System.out.println(ArrayUtils.toString(bools));
    }
}

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar -->
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

How do I convert an array to a Map?

This example use the Apache Commons Lang’s ArrayUtils.toMap() method to convert a two dimensional array into a Map object.

To convert a two dimensional array into a Map object, each element of the two dimensional array must be an array with at least two elements where the first element will be the key and the second element will be the value.

package org.kodejava.example.commons.lang;

import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

public class ArrayToMapExample {

    public static void main(String[] args) {
        // A two dimensional array of countries capital.
        String[][] countries = {{"United States", "Washington, D.C."},
            {"United Kingdom", "London"},
            {"Netherlands", "Amsterdam"},
            {"Japan", "Tokyo"},
            {"France", "Paris"}};

        // Convert an array to a Map.
        Map capitals = ArrayUtils.toMap(countries);

        for (Object key : capitals.keySet()) {
            System.out.printf("%s is the capital of %s.%n", capitals.get(key), key);
        }
    }
}

The result of our code snippet:

London is the capital of United Kingdom.
Amsterdam is the capital of Netherlands.
Paris is the capital of France.
Washington, D.C. is the capital of United States.
Tokyo is the capital of Japan.

Maven Dependencies

<!-- http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar -->
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>