Today, i show you about Java 8 stream min max comparator examples which finds min and max date, number, Char, String or object from stream of comparable elements using Comparator.comparing() method. Java 8 introduced several enhancements to the Comparator interface, including a handful of static functions that are of great utility.
Min or Max Number
Now we have a comparator for integer number like this
1 |
Comparator.comparing(Integer::valueOf) |
Demo program
1 2 3 4 5 6 7 8 9 10 |
public class MinMaxNumberDemo { public static void main(String[] args) { // Find Min or Max Number Integer max = Stream.of(-1, -2, -3, 3, 2, 1).max(Comparator.comparing(Integer::valueOf)).get(); Integer min = Stream.of(-1, -2, -3, 3, 2, 1).min(Comparator.comparing(Integer::valueOf)).get(); System.out.println("max = " + max); System.out.println("min = " + min); } } |
Output
1 2 |
max = 3 min = -3 |
Min or Max String
Now we have a comparator for string like this
1 |
Comparator.comparing(String::valueOf) |
Our demo program
1 2 3 4 5 6 7 8 9 10 |
public class MinMaxStringDemo { public static void main(String[] args) { // Find Min or Max String String maxString = Stream.of("A1", "B1", "C1", "D1").max(Comparator.comparing(String::valueOf)).get(); String minString = Stream.of("A1", "B1", "C1", "D1").min(Comparator.comparing(String::valueOf)).get(); System.out.println("maxString = " + maxString); System.out.println("minString = " + minString); } } |
Output
1 2 |
maxString = D1 minString = A1 |
Min or Max Date
Now we have a comparator for string like this
1 |
Comparator.comparing(LocalDate::toEpochDay) |
Demo program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class MinMaxDateDemo { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); LocalDate endDate = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()); List<LocalDate> dates = Stream.iterate(currentDate, date -> date.plusDays(1)) .limit(ChronoUnit.DAYS.between(currentDate, endDate)) .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(); System.out.println("maxDate = " + maxDate); System.out.println("minDate = " + minDate); } } |
Output
1 2 |
maxDate = 2017-11-29 minDate = 2017-11-23 |
Min or Max Object
We’re considering the POJO like this
1 2 3 4 5 6 7 |
public class People { public int age; public String gender; public String name; //getter and setter } |
And We’re finding the max and min age of people, so our comparator should be such as
1 |
Comparator.comparing(People::getAge) |
Demo program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class MinMaxObjectDemo { public static void main(String[] args) { People a = new People(28, "M", "Mr A"); People b = new People(25, "M", "Mr B"); People c = new People(21, "M", "Mr C"); People d = new People(21, "M", "Mr D"); Collection<People> list = Arrays.asList(a, b, c, d); Comparator<People> comparator = Comparator.comparing(People::getAge); // Find Min or Max age of People object People minAge = list.stream().min(comparator).get(); People maxAge = list.stream().max(comparator).get(); System.out.println("minAge = " + minAge); System.out.println("maxAge = " + maxAge); } } |
Output
That’s all about Java 8 stream min max comparator examples.
References
Comparator (Java Platform SE 8)
Download the complete source code, click link below
StreamMinMaxExamples.zip (155 downloads)