Getting Min and Max values from a Stream | Java 8 Stream min() max()

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Getting Min and Max values from a Stream in Java 8 is not a big deal, here we will see how it can be done.
Here are our use cases to get min and max values from the stream.
// Numbers
Collection<Integer> elements = Arrays.asList(1,2,3,4,5);
Min = 1
Max = 5
//Strings
Collection<String> strings = Arrays.asList("a1","a2","a3","a4");
Min = a1
max = a4
// Objects
Collection<Student> students = Arrays.asList(new Student("Chandra",30),new Student("Rahul",25)
                ,new Student("Robert",20));
Younger = Robert
Elder = Chandra
Finding min and max numbers from an integer stream using stream().min() and stream().max() methods.
// Numbers
Collection<Integer> elements = Arrays.asList(1,2,3,4,5);
Integer min = elements.stream().min(Integer::compareTo).get();
Integer max = elements.stream().max(Integer::compareTo).get();
System.out.println("Min value : "+min);
System.out.println("Max value : "+max);
Output:
Min value : 1
Max value : 5
We can even get the min and max values for string; it internally compares the ASCII values.
// Strings
Collection<String> strings = Arrays.asList("a1","a2","a3","a4");
String min_string = strings.stream().min(String::compareTo).get();
String max_string = strings.stream().max(String::compareTo).get();
System.out.println("Min value : "+min_string);
System.out.println("Max value : "+max_string);
Output:
Min value : a1
Max value : a4
Finding the min and max ages from a list of student objects. Create a Student object.
public class Student{
    private String name;
    private Integer age;

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
Finding elder and younger students based on age.
// Objects
Collection<Student> students = Arrays.asList(new Student("Chandra",30),new Student("Rahul",25)
        ,new Student("Robert",20));
Student younger = students.stream().min((s1,s2)->s1.getAge().compareTo(s2.getAge())).get();
Student elder = students.stream().max((s1,s2)->s1.getAge().compareTo(s2.getAge())).get();
System.out.println("Younger is : "+younger.getName());
System.out.println("Elder is : "+elder.getName());
Output:
Younger is : Robert
Elder is : Chandra
Java 8 Streams
Happy Learning 🙂

Java 8 How to get common elements from two lists
User defined sorting with Java 8 Comparator
Java 8 Array contains a specific value ?
How to Filter null values from Java8 Stream
How to convert List to Map in Java 8
How to get Stream count in Java 8
How to Convert Iterable to Stream Java 8
Java 8 How to Convert List to String comma separated values
How Java 8 Stream generate random String and Numbers
Java 8 How to convert Stream to List
How to Convert Java 8 Stream to Array
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
Java8 Concatenate Arrays Example using Stream
How to Merge Streams in Java 8

Java 8 How to get common elements from two lists
User defined sorting with Java 8 Comparator
Java 8 Array contains a specific value ?
How to Filter null values from Java8 Stream
How to convert List to Map in Java 8
How to get Stream count in Java 8
How to Convert Iterable to Stream Java 8
Java 8 How to Convert List to String comma separated values
How Java 8 Stream generate random String and Numbers
Java 8 How to convert Stream to List
How to Convert Java 8 Stream to Array
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
Java8 Concatenate Arrays Example using Stream
How to Merge Streams in Java 8
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String