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
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
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.
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
Ordering shorthand
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.