In this post, we will see how to find minimum value in a List of Integers in Java 8.
1. Convert List to IntStream
The idea is to convert the list to IntStream and use IntStream#min
method that returns an OptionalInt having the minimum value of the stream. We can either use orElse or orElseGet or orElseThrow to unwrap an OptionalInt to get hold of real Integer inside.
1 2 3 4 5 6 7 8 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() // Stream<Integer> .mapToInt(v -> v) // IntStream .min() // OptionalInt .orElse(Integer.MAX_VALUE); // Integer } |
2. Stream.min()
We can also use min()
provided by Stream which accepts a Comparator to compare items in the stream against each other and returns an Optional having the minimum value in the stream.
1 2 3 4 5 6 7 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() // Stream<Integer> .min(Comparator.naturalOrder()) // Optional<Integer> .get(); // Integer } |
Now since Integer#compare
takes two ints as argument and returns an int value similar to a Comparator#compare
, it complies with Comparator functional interface.
1 2 3 4 5 6 7 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() // Stream<Integer> .min(Integer::compare) // Optional<Integer> .get(); // Integer } |
3. Reduction operation
We can also perform a reduction operation on the values of the stream using Integer#min
function, which then returns an Optional describing the minimum value present.
1 2 3 4 5 6 7 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() // Stream<Integer> .reduce(Integer::min) // Optional<Integer> .get(); // Integer } |
There’s an overloaded version of reduce()
method that performs a reduction on the values of the stream, using the provided identity value and an associative accumulation function and returns the reduced value.
1 2 3 4 5 6 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() .reduce(Integer.MAX_VALUE, Integer::min); } |
4. Using Collectors
We can also use Collectors to find the the minimum value in the list.
1. Collectors#minBy
returns a Collector that produces the minimal value according to a given Comparator.
1 2 3 4 5 6 7 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() .collect(Collectors.minBy(Comparator.naturalOrder())) .get(); } |
2. Collectors#summarizingInt
returns a Collector which applies an int-producing mapping function to each input value and returns summary statistics for the resulting values containing statistics such as count, min, max, sum and average.
1 2 3 4 5 6 7 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() .collect(Collectors.summarizingInt(Integer::intValue)) .findMin(); } |
3. Collectors#reducing
returns a Collector which performs reduction of its input values under a specified BinaryOperator and returns an Optional.
1 2 3 4 5 6 7 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() .collect(Collectors.reducing(Integer::min)) .get(); } |
5. Using Sorting
If we sort the stream in natural order, then the first value in the stream would be the minimum value. This approach is not recommened as it is very inefficient.
1 2 3 4 5 6 7 8 |
// Function to find minimum value in a List in Java 8 public static Integer findMin(List<Integer> list) { return list.stream() .sorted() .findFirst() .get(); } |
In order to avoid NullPointerException if the given list is null and NoSuchElementException if given list is empty, we can add below code to all above methods in the beginning:
1 2 |
if (list == null || list.size() == 0) return Integer.MAX_VALUE; |
References:
1. Stream Javadoc SE 8
2. Collectors Javadoc SE 8
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply