Min value in list

This example will find the least or minimum element of the specified ArrayList using java, java8, guava and apache commons. It will find the opposite of maximum integer value in arraylist and resembles the same behavior as finding the min value in array. For our set up data, we randomly choose American Football center's rookie year and created a List of Integers. A comparable example demonstrates how to get the smallest value in arraylist using groovy.

Setup

private static List<Integer> CENTERS_ROOKIE_YEAR = Lists.newArrayList(
        1946, 1988, 1970, 1931,
        1940, 1920, 1980, 1953,
        1960, 1974, 1987);

Straight up Java

In this snippet we will use core jdk Collections, a class that consists of static methods that operate on collections, to find the min element. Collections.min will return the min element according to the natural ordering of the elements, in this case returning 1920 from CENTERS_ROOKIE_YEAR

@Test
public void find_min_value_from_list_of_integers_java () {

    Integer minElement = Collections.min(CENTERS_ROOKIE_YEAR);

    assertEquals(new Integer (1920), minElement);
}

Java 8

This snippet will use java 8 stream reduction to find the minimum integer in the array list. Calling stream() method we will transform the list to a stream then mapToInt function to convert the stream to an IntStream, a specialized stream for dealing with primitive ints. Finally calling the min method which will return an OptionalInt describing the min value found or an empty OptionalInt if the stream is empty. Looking for more than just the min, IntSummaryStatistics is a class that calculates all statistics such as average, count, min and sum.

@Test
public void find_min_value_from_list_of_integers_java8() {

    OptionalInt minElement = CENTERS_ROOKIE_YEAR.stream().mapToInt(p -> p)
            .min();

    assertEquals(1920, minElement.getAsInt());

    // or

    Optional<Integer> minElement2 = CENTERS_ROOKIE_YEAR.stream().reduce(
            Integer::min);

    assertEquals(new Integer(1920), minElement2.get());

}

Google Guava

In both of guava's snippets below, we will use the Ordering class which is a fluent Comparator class to call the min method which will return the smallest value according to this orderings.

Ordering

@Test
public void find_min_value_from_list_of_integers_guava_ordering() {

    Ordering<Integer> byYear = new Ordering<Integer>() {
        @Override
        public int compare(Integer left, Integer right) {
            return Ints.compare(left, right);
        }
    };

    assertEquals(new Integer (1920), byYear.min(CENTERS_ROOKIE_YEAR));
}

Ordering shorthand

@Test
public void find_min_value_from_list_of_integers_guava_ordering_shorthand () {

    Integer minElement = Ordering.natural().min(CENTERS_ROOKIE_YEAR);

    assertEquals(new Integer (1920), minElement);
}

Apache Commons

This snippet will use apache commons ObjectUtils, a class that performs operations on objects, calling the min() method to find the min value in the ArrayList of Integers.

@Test
public void find_min_value_from_list_of_integers_apache () {

    Integer minElement = ObjectUtils.min(
                CENTERS_ROOKIE_YEAR.toArray(
                        new Integer[CENTERS_ROOKIE_YEAR.size()]));

    assertEquals(new Integer (1920), minElement);
}