April 28, 2024

Java Stream max() and min() With Examples

Java Stream API provides two methods max() and min() where-

  • max()- Returns the maximum element of this stream according to the provided Comparator.
  • min()- Returns the minimum element of this stream according to the provided Comparator.

Java Stream max() method

Syntax of the max() method in Java Stream API is as given below

Optional<T> max(Comparator<? super T> comparator)

Method returns an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty. This is a terminal operation.

max() method Java examples

1. In this simple example we’ll use a List as a source of Stream and get the maximum element of the List.

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class MaxDemo {

  public static void main(String[] args) {
    // Till Java 8
    //List<Integer> myList = Arrays.asList(11, 1, 9, 4, 98, 0, 17, 8, 2, 3);
    // From Java 9
    List<Integer> myList = List.of(11, 1, 9, 4, 98, 0, 17, 8, 2, 3);
    Optional<Integer> max = myList.stream().max(Integer::compare);
    if(max.isPresent()){
      System.out.println("Maximum element in the List " + max.get());
    }
  }
}
Output
Maximum element in the List 98

2. In this example we’ll get the maximum salary from the List of Employee objects.

Employee class used is as given below.

public class Employee{
  private String name;
  private String dept;
  private int salary;
  private int age;
  Employee(String name, String dept, int salary, int age){
    this.name = name;
    this.dept = dept;
    this.salary = salary;
    this.age = age;
  }
  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getDept() {
    return dept;
  }
  public void setDept(String dept) {
    this.dept = dept;
  }
  public int getSalary() {
    return salary;
  }
  public void setSalary(int salary) {
    this.salary = salary;
  }
  @Override
  public String toString() {
    // TODO Auto-generated method stub
    return "Name- " + getName() + " Dept- " + getDept() + 
        " Salary- " + getSalary();
  }
}

To get the maximum salary you can first use mapToInt() method to get an IntStream consisting of employee salaries, then use max() method.

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;

public class MaxDemo {

  public static void main(String[] args) {

    List<Employee> empList =  getEmployeeList();
    OptionalInt maxSal = empList.stream()
                                .mapToInt(Employee::getSalary)
                                .max();
    if(maxSal.isPresent()){
      System.out.println("Maximum Salary: " + maxSal.getAsInt());
    }
  }
  
    // Method to create list of employee objects
    private static List<Employee> getEmployeeList(){
        List<Employee> empList = Arrays.asList(new Employee("Ram", "IT", 12000, 34), 
                                       new Employee("Tina", "HR", 15000, 42), 
                                       new Employee("Roger", "IT", 9000, 25), 
                                       new Employee("Troy", "Accounts", 7000, 35));
        
        return empList;
    }
}
Output
Maximum Salary: 15000

If you want the employee object for the employee having maximum salary that can be done as given below-

Optional emp = empList.stream().max(Comparator.comparing(Employee::getSalary));
if(emp.isPresent()){
    System.out.println("Employee With Maximum Salary: " + emp.get());
}

Java Stream min() method

Syntax of the min() method in Java Stream API is as given below.

Optional<T> min(Comparator<? super T> comparator)

This method returns an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty. This is a terminal operation.

min() method Java examples

1. In this simple example we’ll use a List as a source of Stream and get the minimum element of the List.

import java.util.List;
import java.util.Optional;

public class MinDemo {
  public static void main(String[] args) {
    // Till Java 8
    //List<Integer> myList = Arrays.asList(11, 1, 9, 4, 98, 0, 17, 8, 2, 3);
    // From Java 9
    List<Integer> myList = List.of(11, 1, 9, 4, 98, 0, 17, 8, 2, 3);
    Optional<Integer> max = myList.stream().min(Integer::compare);
    if(max.isPresent()){
      System.out.println("Minimum element in the List " + max.get());
    }
  }
}
Output
Minimum element in the List 0

2. From the list of employees get the youngest employee. Employee class used is same as already displayed above.

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;

public class MinDemo {
  public static void main(String[] args) {

    List<Employee> empList =  getEmployeeList();
    OptionalInt minAge = empList.stream()
                                .mapToInt(Employee::getAge)
                                .min();
    if(minAge.isPresent()){
      System.out.println("Employee with minimum age: " + minAge.getAsInt());
    }
  }
  
    // Method to create list of employee objects
    private static List<Employee> getEmployeeList(){
        List<Employee> empList = Arrays.asList(new Employee("Ram", "IT", 12000, 34), 
                                       new Employee("Tina", "HR", 15000, 42), 
                                       new Employee("Roger", "IT", 9000, 25), 
                                       new Employee("Troy", "Accounts", 7000, 35));
        
        return empList;
    }
}
Output
Employee with minimum age: 25

That's all for the topic Java Stream max() and min() With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment