Find element index in array

This example will find the index of a given primitive or object value within an array while using java, guava and apache commons.

Straight up Java

While using the core jdk we will create an array of Strings and Integers then call Arrays.asList to convert to an arraylist. Next we will call ArrayList.indexOf which will return the index of the first occurrence of the specified element in this list.

Object

@Test
public void element_index_in_array_java () {
    
    String[] lilyFlowers = {   
              "Lily of the valley",
              "Lily Elite",
              "Lily Monte Negro",
              "Lily Casa Blanca",
              "Lily of the Nile – Alba",
              "Lily Stargazer"};
    
    int indexOfFlower = Arrays.asList(lilyFlowers).indexOf("Lily Monte Negro");
    
    assertEquals(2, indexOfFlower);  
}

Primitive

@Test 
public void element_index_in_int_array_java () {

    Integer[] twoQuarters = {1, 2, 3, 4, 5, 6};
    
    int endOfFirstQuarter = Arrays.asList(twoQuarters).indexOf(3);
    
    assertEquals(2, endOfFirstQuarter);
}

Google Guava

Object

Using guava's Iterators class we will pass a predicate to Iterators.indexOf which will return the index of the first element that satisfies the provided predicate or in this case that matches "Lily Elite".

@Test
public void element_index_in_array_java_with_guava () {
    
    String[] lilyFlowers = {   
              "Lily of the valley",
              "Lily Elite",
              "Lily Monte Negro",
              "Lily Casa Blanca",
              "Lily of the Nile – Alba",
              "Lily Stargazer"};

    int indexOfFlower = Iterators.indexOf(Iterators.forArray(lilyFlowers), new Predicate<String>() {
        public boolean apply(String input) {
            return input.equals("Lily Elite");
        }
    });
    
    assertEquals(1, indexOfFlower);  
}

Primitive

To find the index of primitive array using guava, we will use Ints class that contains static utility methods pertaining to int primitives. The Ints.indexOf will return the index of the first apperance of the value specified or in this case 3.

@Test
public void element_index_of_ints_in_array_java_with_guava () {
    
    int [] twoQuarters = {1, 2, 3, 4, 5, 6};
    
    int lastMonthInFirstQuarter = Ints.indexOf(twoQuarters, 3);
    assertEquals(2, lastMonthInFirstQuarter);
}

Apache Commons

Apache commons ArrayUtils class contains operations on primitive or object arrays. We will ArrayUtils.indexOf which will find the index of the given value in the array.

Object

@Test
public void element_index_of_string_array_java_with_apache_commons() {

    String[] lilyFlowers = { "Lily of the valley", "Lily Elite",
            "Lily Monte Negro", "Lily Casa Blanca",
            "Lily of the Nile – Alba", "Lily Stargazer" };

    int indexOfFlower = ArrayUtils.indexOf(lilyFlowers, "Lily Elite");

    assertEquals(1, indexOfFlower);
}

Primitive

@Test
public void element_index_of_int_array_java_with_apache_commons() {

    int [] twoQuarters = {1, 2, 3, 4, 5, 6};
    
    int lastMonthInFirstQuarter = ArrayUtils.indexOf(twoQuarters, 3);
    assertEquals(2, lastMonthInFirstQuarter);
}