Finding Max and Min from List using Streams

Learn to find min and max values from a List using Stream API e.g. a date, number, Char, String or an object. We will use the Comparator.comparing() for custom comparison logic.

1. Overview

We will be using the following functions to find the max and min values from the stream:

  • Stream.max(comparator) : It is a terminal operation that returns the maximum element of the stream according to the provided Comparator.
  • Stream.min(comparator) : It is a terminal operation that returns the minimum element of the stream according to the provided Comparator.

2. Finding Min or Max Date

To get max or min date from a stream of dates, you can use Comparator.comparing( LocalDate::toEpochDay ) Comparator. The toEpochDay() function returns the count of days since epoch i.e. 1970-01-01.

LocalDate start = LocalDate.now();
LocalDate end = LocalDate.now().plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
 
//Create stream of dates
List<LocalDate> dates = Stream.iterate(start, date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(start, end))
                .collect(Collectors.toList());
 
// Get Min or Max Date
LocalDate maxDate = dates.stream()
              .max( Comparator.comparing( LocalDate::toEpochDay ) )
              .get();
 
LocalDate minDate = dates.stream()
              .min( Comparator.comparing( LocalDate::toEpochDay ) )
              .get();

Use the above program to find the earliest date or latest date from a list of dates.

3. Find Min or Max Number

To find min and max numbers from the stream of numbers, use Comparator.comparing( Integer::valueOf ) like comparators. The below example is for a stream of Integers.

// Get Min or Max Number
Integer maxNumber = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
          .max(Comparator.comparing(Integer::valueOf))
          .get();
 
Integer minNumber = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
          .min(Comparator.comparing(Integer::valueOf))
          .get();

4. Find Min or Max Char or String

To find min and max string or char from a stream of chars, use Comparator.comparing( String::valueOf ) like comparators.

// Get Min or Max String/Char
String maxChar = Stream.of("H", "T", "D", "I", "J")
            .max(Comparator.comparing(String::valueOf))
            .get();
 
String minChar = Stream.of("H", "T", "D", "I", "J")
            .min(Comparator.comparing(String::valueOf))
            .get();

5. Find Min or Max Object by Field Value

The Object comparison involves creating our own custom comparator, first. For example, if I want to get the youngest employee from a stream of Employee objects, then my comparator will look like Comparator.comparing(Employee::getAge). Now use this comparator to get max or min employee object.

Java program to find max or min employee object by their age.

Find max or min object by object property
List<Employee> employees = new ArrayList<Employee>();

//add few employees
 
Comparator<Employee> comparator = Comparator.comparing( Employee::getAge );
 
// Get Min or Max Object
Employee minObject = employees.stream().min(comparator).get();
Employee maxObject = employees.stream().max(comparator).get();

6. Conclusion

In this tutorial, we learned to find max value or min value from a list using the Java stream API and lambda expression. We also learned to find max or min objects such as max Date or String.

We also learned to find the max object by object property from the stream of objects.

Happy Learning !!

Sourcecode on Github

Leave a Reply

7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial