Max value in list

Opposite of finding the minimum value in arraylist and resembling the same behavior as finding the max value in array, this example will find the greatest value present or max value in an ArrayList using java, java 8, guava and apache commons. 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 largest 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 max element. Collections.max will return the max element according to the natural ordering of the elements, in this case returning 1988 from CENTERS_ROOKIE_YEAR

@Test
public void find_max_value_from_list_of_integers_java () {

    Integer maxElement = Collections.max(CENTERS_ROOKIE_YEAR);

    assertEquals(new Integer (1988), maxElement);
}

Java 8

This snippet will use java 8 stream reducing technique to find the maximum integer in the array list. Calling stream() method we will convert 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 max method which will return an OptionalInt describing the max value found or an empty OptionalInt if the stream is empty. Looking for more than just the max, IntSummaryStatistics is a class that calculates all statistics such as average, count, min and sum.

@Test
public void find_max_value_from_list_of_integers_java8() {

    OptionalInt maxElement = CENTERS_ROOKIE_YEAR.stream().mapToInt(p -> p)
            .max();

    assertEquals(1988, maxElement.getAsInt());

    // or

    Optional<Integer> maxElement2 = CENTERS_ROOKIE_YEAR.stream().reduce(
            Integer::max);

    assertEquals(new Integer(1988), maxElement2.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 max method which will return the greatest value according to this orderings.

Ordering

@Test
public void find_max_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 (1988), byYear.max(CENTERS_ROOKIE_YEAR));
}

Ordering shorthand

@Test
public void find_max_value_from_list_of_integers_guava_ordering_shorthand () {

    Integer maxElement = Ordering.natural().max(CENTERS_ROOKIE_YEAR);

    assertEquals(new Integer (1988), maxElement);
}

Apache Commons

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

@Test
public void find_max_value_from_list_of_integers_apache () {

    Integer maxElement = ObjectUtils.max(
            CENTERS_ROOKIE_YEAR.toArray(
                    new Integer[CENTERS_ROOKIE_YEAR.size()]));

    assertEquals(new Integer (1988), maxElement);
}