Stream min() and max() are intermediate operations in Java used to find the min/max value from a Stream of comparable elements. Both methods accept Comparator as an input and return Optional.
You will learn what Optional class is in upcoming tutorials.
Java Stream min() operation
Syntax
Optional<T> min(Comparator<? super T> comparator);
Example 1:
Get the minimum number from a stream of integers:
class Test {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 98, 72, 48, 3, 10, 14, 42, 97, 24));
int minNumber = numbers.stream()
.min(Comparator.comparing(Integer::valueOf))
.get();
System.out.println("Minimum number is: " + minNumber);
}
}
Output: Minimum number is: 3
The Comparator.comparing() method compares the stream elements based on the input field. In this case, it will compare the integers based on the value of each element.
Example 2:
Get the student with the lowest grade.
Let’s create a Student class first:
class Student {
private String firstName;
private String lastName;
private int grade;
public Student(String firstName, String lastName, int grade) {
this.firstName = firstName;
this.lastName = lastName;
this.grade = grade;
}
public int getGrade() {
return this.grade;
}
@Override
public String toString() {
return "student: { firstName: " + firstName + ", lastName: " + lastName + ", grade: " + grade + " }";
}
}
Now, let’s write the program to return the student with the lowest grade:
class Test {
public static void main(String[] args) {
Student student = getStudents().stream()
.min(Comparator.comparing(Student::getGrade))
.get();
System.out.println("Student with the lowest grade: " + student);
}
private static List<Student> getStudents() {
List<Student> students = new ArrayList<>();
students.add(new Student("Steve", "Rogers", 8));
students.add(new Student("John", "Doe", 5));
students.add(new Student("Melissa", "Smith", 7));
students.add(new Student("Megan", "Norton", 4));
students.add(new Student("Tom", "Johnson", 9));
return students;
}
}
Output: Student with the lowest grade: student: { firstName: Megan, lastName: Norton, grade: 4 }
Note: the get() method returns the value that the Optional is holding.
Java Stream max() operation
Syntax
Optional<T> max(Comparator<? super T> comparator);
Example 1:
Get the max number from the stream of integers:
class Test {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 98, 72, 48, 3, 10, 14, 42, 97, 24));
int maxNumber = numbers.stream()
.max(Comparator.comparing(Integer::valueOf))
.get();
System.out.println("Maximum number is: " + maxNumber);
}
}
Output: Maximum number is: 98
Example 2:
Get the student with the highest grade.
We will use the same Student class from the above example, and we will write a program that returns the student with the highest grade using the max() method.
class Test {
public static void main(String[] args) {
Student student = getStudents().stream()
.max(Comparator.comparing(Student::getGrade))
.get();
System.out.println("Student with the highest grade: " + student);
}
private static List<Student> getStudents() {
List<Student> students = new ArrayList<>();
students.add(new Student("Steve", "Rogers", 8));
students.add(new Student("John", "Doe", 5));
students.add(new Student("Melissa", "Smith", 7));
students.add(new Student("Megan", "Norton", 4));
students.add(new Student("Tom", "Johnson", 9));
return students;
}
}
Output: Student with the highest grade: student: { firstName: Tom, lastName: Johnson, grade: 9 }
I hope this tutorial was helpful to you. To learn more, check out other Java Functional Programming tutorials.