In this post, we will learn “How to find Max & Min from a List using Java 8 Streams API?”.
Here, we are going to learn following things:
- How to Find Max & Min Number in a List?
- How to Find Max & Min String in a List?
- How to Find Max & Min Object by Key in a List?
Find Max or Min from a List using Java 8 Streams...!!! Share on X
Let’s Begin,
1. Find Max & Min Number in a List
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.OptionalInt;
public class Find_Max_Min_Integer_ArrayList_Java8 {
public static void main(String[] args) {
List <Integer> list = Arrays.asList(40, 32, 53, 22, 11, 89, 76);
//1st way of finding maximum value
OptionalInt maxNumber1 = list.stream()
.mapToInt(value -> value)
.max();
System.out.println("Maximum Value Element in the List: " + maxNumber1);
//2nd way of finding maximum value
int maxNumber2 = list.stream()
.mapToInt(value -> value)
.max()
.orElseThrow(NoSuchElementException::new);
System.out.println("Maximum Value Element in the List: " + maxNumber2);
//3rd way of finding maximum value
int maxNumber3 = list.stream()
.max(Comparator.comparing(Integer::valueOf))
.get();
System.out.println("Maximum Value Element in the List: " + maxNumber3);
//1st way of finding minimum value
OptionalInt minNumber1 = list.stream()
.mapToInt(value -> value)
.min();
System.out.println("Minimum Value Element in the List: " + minNumber1);
//2nd way of finding minimum value
int minNumber2 = list.stream()
.mapToInt(value -> value)
.min()
.orElseThrow(NoSuchElementException::new);
System.out.println("Minimum Value Element in the List: " + minNumber2);
//3rd way of finding minimum value
int minNumber3 = list.stream()
.min(Comparator.comparing(Integer::valueOf))
.get();
System.out.println("Minimum Value Element in the List: " + minNumber3);
}
}
Output:
Maximum Value Element in the List: OptionalInt[89] Maximum Value Element in the List: 89 Maximum Value Element in the List: 89 Minimum Value Element in the List: OptionalInt[11] Minimum Value Element in the List: 11 Minimum Value Element in the List: 11
2. Find Max & Min String in a List
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
public class Find_Max_Min_String_ArrayList_Java8 {
public static void main(String[] args) {
List <String> list = Arrays.asList("Tom", "Henry", "Adam", "Sandy", "Ross");
//1st way of finding maximum char
Optional <String> maxChar1 = list.stream()
.max(Comparator.comparing(String::valueOf));
System.out.println("Maximum Character Element in the List: " + maxChar1);
//2nd way of finding maximum char
String maxChar2 = list.stream()
.max(Comparator.comparing(String::valueOf))
.orElseThrow(NoSuchElementException::new);
System.out.println("Maximum Character Element in the List: " + maxChar2);
//1st way of finding minimum char
Optional <String> minChar1 = list.stream()
.min(Comparator.comparing(String::valueOf));
System.out.println("Minimum Character Element in the List: " + minChar1);
//2nd way of finding maximum char
String minChar2 = list.stream()
.min(Comparator.comparing(String::valueOf))
.orElseThrow(NoSuchElementException::new);
System.out.println("Minimum Character Element in the List: " + minChar2);
}
}
Output:
Maximum Character Element in the List: Optional[Tom] Maximum Character Element in the List: Tom Minimum Character Element in the List: Optional[Adam] Minimum Character Element in the List: Adam
3. Find Max & Min Object by Key in a List
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Find_Max_Min_Object_By_Key_ArrayList_Java8 {
public static void main(String[] args) {
List < Person > persons = Arrays.asList(
new Person("Tom", 3),
new Person("Sam", 5),
new Person("Andy", 12),
new Person("Jazz", 18),
new Person("Brett", 9)
);
//Get Max age Person info
Person maxPerson = persons.stream()
.max(Comparator.comparing(Person::getAge))
.get();
System.out.printf("%s has the maximum age among the list which is %s", maxPerson.getName(), maxPerson.getAge());
System.out.println("\n");
//Get Min age Person info
Person minPerson = persons.stream()
.min(Comparator.comparing(Person::getAge))
.get();
System.out.printf("%s has the minimum age among the list which is %s", minPerson.getName(), minPerson.getAge());
}
}
Person (POJO class):
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
Output:
Jazz has the maximum age among the list which is 18 Tom has the minimum age among the list which is 3
Find Max or Min from a List using Java 8 Streams...!!! Share on X
Do you like this Post? – then check my other helpful posts: