Max and Min operation on Stream in Java8

The most frequent operation while working with collections is to iterate over a collection and perform some action on each element in turn. For example, Consider we have a List of Employees and we want to find the senior most and junior most employee from the List. So in normal boilerplate way we need to iterate over the List and identify the employees with maximum and minimum age. As we already knew that Streams allow us to write collections-processing code at a higher level of abstraction.  Fortunately, this case is very well covered by the max and min operations that are provided by the Streams API. Consider the following example of finding the senior most and junior most employee from the List.

package com.tuturself;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class MaxMinStreamExample {

	public static void main(String[] args) {
		List<Emp> employeeList = new ArrayList<>();
		employeeList.add(new Emp(1, "Ninja Panda", 32));
		employeeList.add(new Emp(2, "Ninja AK", 35));
		employeeList.add(new Emp(3, "Master Shifu", 30));
		employeeList.add(new Emp(3, "Master Piku", 29));

		Emp junior = employeeList.stream()
				.min(Comparator.comparing(emp -> emp.getAge())).get();
		System.out.println("Junior Most Employee is :" + junior);

		Emp senior = employeeList.stream()
				.max(Comparator.comparing(emp -> emp.getAge())).get();
		System.out.println("Senior Most Employee is :" + senior);

	}
}

class Emp {

	private int employeeId;
	private String name;
	private int age;

	public Emp(int employeeId, String name, int age) {
		this.employeeId = employeeId;
		this.name = name;
		this.age = age;
	}

	public int getEmployeeId() {
		return this.employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return this.age;
	}

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

	@Override
	public String toString() {
		return "Emp [employeeId=" + employeeId + ", " + 
                           "name=" + name + ", age=" + age + "]";
	}
}

When we think about maximum and minimum elements, the first thing we need to think about is the ordering that we’re going to be using. When it comes to finding the junior employee, the ordering is provided by the age of the employees. In order to inform the Stream that we’re using the age of the employee, we give it a Comparator. Conveniently, Java 8 has added a static method called comparing that lets us build a comparator using keys. Previously, we always encountered an ugly pattern in which we had to write code that got a field out of both the objects being compared, then compare these field values. Now, to get the same element out of both elements being compared, we just provide a getter function for the value. In this case we’ll use age, which is a getter function in disguise. It’s worth reflecting on the comparing method for a moment. This is actually a function that takes a function and returns a function.

core java 12 Java-8 12 Stream 12

FOLLOW US ON LinkedIn



Explore Tutu'rself