Thursday 26 March 2020

Streams: max(): Get maximum value

Stream interface provides 'max' method, it takes a comparator and return the maximum element of this stream according to the provided comparator.

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

Example
Integer highestNumber = numbers.stream().max(Integer::compare).get();

App.java
package com.sample.app;

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

public class App {

 public static void main(String args[]) {
  List<Integer> numbers = Arrays.asList(11, 3, 43, 23, 213, 32, 54, 67, 89);

  Integer highestNumber = numbers.stream().max(Integer::compare).get();

  System.out.println("Highest Number : " + highestNumber);

 }

}

Output
Highest Number : 213

Let’s try to get the student with highest age and student with last name using max() function.

Let's write name and age comparator like below.
Comparator<Student> nameComparator = new Comparator<Student>() {

 @Override
 public int compare(Student s1, Student s2) {
  return s1.getName().compareTo(s2.getName());
 }

};

Comparator<Student> ageComparator = new Comparator<Student>() {

 @Override
 public int compare(Student s1, Student s2) {
  int x = s1.getAge();
  int y = s2.getAge();

  return (x < y) ? -1 : ((x == y) ? 0 : 1);

 }

};

How to get the student with name is at last (Descending Order)?
Student studentWithLastName = students.stream().max(nameComparator).get();
                 
How to get the student with highest age?
Student studentWithHighestAge = students.stream().max(ageComparator).get();

Find the below working application.

Student.java
package com.sample.app.model;

public class Student {
 private int id;
 private String name;
 private int age;

 public Student(int id, String name, int age) {
  this.id = id;
  this.name = name;
  this.age = age;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

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

 public int getAge() {
  return age;
 }

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

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Student [id=");
  builder.append(id);
  builder.append(", name=");
  builder.append(name);
  builder.append(", age=");
  builder.append(age);
  builder.append("]");
  return builder.toString();
 }

}

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.sample.app.model.Student;

public class App {

 public static void main(String args[]) {
  Student s1 = new Student(1, "Ram", 22);
  Student s2 = new Student(2, "Joel", 18);
  Student s3 = new Student(3, "Gopi", 17);
  Student s4 = new Student(4, "Bhargavi", 23);
  Student s5 = new Student(5, "Gowthami", 20);
  Student s6 = new Student(6, "Jhansi", 25);

  List<Student> students = Arrays.asList(s1, s2, s3, s4, s5, s6);

  Comparator<Student> nameComparator = new Comparator<Student>() {

   @Override
   public int compare(Student s1, Student s2) {
    return s1.getName().compareTo(s2.getName());
   }

  };

  Comparator<Student> ageComparator = new Comparator<Student>() {

   @Override
   public int compare(Student s1, Student s2) {
    int x = s1.getAge();
    int y = s2.getAge();

    return (x < y) ? -1 : ((x == y) ? 0 : 1);

   }

  };

  Student studentWithLastName = students.stream().max(nameComparator).get();
  Student studentWithHighestAge = students.stream().max(ageComparator).get();

  System.out.println("studentWithLastName : " + studentWithLastName);
  System.out.println("studentWithHighestAge : " + studentWithHighestAge);
 }

}

Output
studentWithLastName : Student [id=1, name=Ram, age=22]
studentWithHighestAge : Student [id=6, name=Jhansi, age=25]



Previous                                                    Next                                                    Home

No comments:

Post a Comment