Stream interface provides 'min' method, it takes a comparator and return the minimum element of this stream according to the provided comparator.
Signature
Optional<T> min(Comparator<? super T> comparator);
Example
Integer minimumNumber = numbers.stream().min(Integer::compare).get();
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 minimumNumber = numbers.stream().min(Integer::compare).get();
System.out.println("Minimum Number : " + minimumNumber);
}
}
Output
Minimum Number : 3
Let’s try to get the student with minimum age and student with starting name using min() 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 starting name?
Student studentWithStartName = students.stream().min(nameComparator).get();
How to get the student with least age?
Student studentWithMinAge = students.stream().min(ageComparator).get();
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 studentWithStartName = students.stream().min(nameComparator).get();
Student studentWithMinAge = students.stream().min(ageComparator).get();
System.out.println("studentWithStartName : " + studentWithStartName);
System.out.println("studentWithMinAge : " + studentWithMinAge);
}
}
Output
studentWithStartName : Student [id=4, name=Bhargavi, age=23]
studentWithMinAge : Student [id=3, name=Gopi, age=17]
No comments:
Post a Comment