Get Highest Score from ArrayList Using Java Stream

java get max value from list of objects
find max value in hashmap java 8
java lambda get max value from list
java stream max
how to get latest date from list in java 8
java 8 list get max
find largest value in arraylist java
get max value from array list

I would like to get the highest score group by Id .If two highest score's are same then i would like get the highest score based on lowest Optional ID.I would like to get it in Java Stream.So far i am trying the following codes which is not working Example :

Array List:

ID: 1 Score:80 OptionalId:1 ID: 1 Score:90 OptionalId:2 ID: 1 Score:90 OptionalId:3 ID: 2 Score:80 OptionalId:1 ID: 2 Score:100 OptionalId:3 ID: 2 Score:100 OptionalId:5

The result should be

ID: 1 Score 90 OptionalId:2 ID 2 Score 100 OptionalId:3

Map<Long, Optional<Person>> result1 =

        for(Person ns: result1) {


How to get maximum value from the Collection (for example , import java.util.Comparator public int getMax(ArrayList list){ int max = Integer. Here are three more ways to find the maximum value in a list, using streams: Using Collectors.toCollection() method: Approach: Get the Stream to be converted. Collect the stream as ArrayList using collect() and Collectors.toCollection() methods.

For a given Id value, there must be a Person. Existence of the Id value solely depends on the Person. So if there exists an Id, there must be a Person too. Therefore what is the point of having an Optional<Person> as the value of the map. In contrast, it makes more sense to merely have a Person instance as the value in the map. Here I am using the toMap collector in concert with the BinaryOperator.maxBy to get the work done. Here's how it looks. Notice how the BinaryOperator.maxBy is used as the mergeFunction.

Map<Integer, Person> maxPersonById =
    .collect(Collectors.toMap(Person::getId, Function.identity(),

And here's the output for the above given input.

{1=Person [id=1, score=90, optionalId=2], 2=Person [id=2, score=100, optionalId=3]}

Get Highest Score from ArrayList Using Java Stream, Get Highest Score from ArrayList Using Java Stream. Vis Team January 27, 2019​. I would like to get the highest score group by Id .If two highest score's are  ArrayList stream. We can also get a stream from an ArrayList. The stream() method on an ArrayList instance returns a Stream object with a matching type.ArrayList. However: Stream() does not return an IntStream or DoubleStream, even when those types are present. So: We must use the stream() directly when we get it from an ArrayList. We "cannot

You may try the following stream code which aggregates by ID, and then finds the max score using a two level sort, first by score, then, by optional ID in case of a tie for score:

import static java.util.Collections.reverseOrder;
import static java.util.Comparator.comparing;

Map<Long, Optional<Person>> result1 =

Optional[ID: 1 Score: 90 OptionalId: 2]
Optional[ID: 2 Score: 100 OptionalId: 3]

The trick here is to reverse the sorting order for only the optional ID, which we want to be ascending, not descending. The sort order by default would be descending, because we are invoking Collections.maxBy.

I cite this great SO question for help with the reverse syntax. Also, I borrowed the boiler plate code from @mayamar to setup the following demo:


(demo only for demonstration purposes)

Find Maximum and Minimum of Custom Objects in Java, The idea is to convert the list of objects into a Stream of objects and then use We can also use Collectors to find the maximum or minimum object in the list. If the Player objects naturally sort themselves using the getTotalScore() attribute, consider making Player implement Comparable<Player>, using code similar to the above. Then to find the highest scorer, you would just call Collections.max(playerList); (without having to provide a Comparator). share | improve this answer

I changed a bit and introduced a helper class that compares score and optionalId.

public class T21Group {

public static void main(String[] args) {
    List<Person> records = new ArrayList<>();
    records.add(new Person(1, 80, 1));
    records.add(new Person(1, 90, 2));
    records.add(new Person(1, 90, 3));
    records.add(new Person(2, 80, 1));
    records.add(new Person(2, 100, 3));
    records.add(new Person(2, 100, 5));

    Map<Long, Optional<Person>> result1 =
            .collect(Collectors.groupingBy(Person::getId, Collectors.maxBy(Comparator.comparing(Pair::new))));

    for (Optional<Person> ns : result1.values()) {

public static class Pair implements Comparable<Pair> {
    long score;
    long optionalId;

    public Pair(Person p) {
        score = p.getScore();
        optionalId = p.getOptionalId();

    public int compareTo(Pair o) {
        if (this.score == o.score) {
            return, this.optionalId);
        return, o.score);


public static class Person {
    private long id;
    private long score;
    private long optionalId;

    public Person(long id, long score, long optionalId) { = id;
        this.score = score;
        this.optionalId = optionalId;

    public String toString() {
        return "ID: " + id + " Score: " + score + " OptionalId: " + optionalId;

    public long getId() {
        return id;

    public void setId(long id) { = id;

    public long getScore() {
        return score;

    public void setScore(long score) {
        this.score = score;

    public long getOptionalId() {
        return optionalId;

    public void setOptionalId(long optionalId) {
        this.optionalId = optionalId;



Finding minimum and maximum element of a Collection in Java , These are static method of Collections Class in Java. Hence they Get the ArrayList List: [12, 53, 30, 8] Minimum value of list is: 8 Maximum value of list is: 53. ArrayList cannot be used for primitive datatypes like int, float, char etc, It uses objects but it can use these primitive datatypes with the help of wrapper class in java. In ArrayList, addition of the elements does not maintain the same sequence they may array in any order.

Java Program to find largest element in an array, Given an array, find the largest element in it. You can simply use the new Java 8 Streams but you have to work with int. int max = Stream.max () The idea is to convert the list into a Stream and call Stream#max () that accepts a Comparator to compare items in the stream against each other to find the maximum element, and returns an Optional containing the maximum element in the stream according to the provided Comparator. // Function to find maximum element from a List of Integers in Java 8 and above public static Integer getMax (List<Integer> list) { return () // Stream<Integer> .max (Comparator.

Getting object with min/max value property from list of objects in Java 8, import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class GetMinMaxFromObjectList { For example, if I want to get the youngest employee from a stream of Employee objects, then my comparator will look like Comparator.comparing(Employee::getAge). Now use this comparator to get max or min employee object. Java program to find max or min employee object by it’s age.

How to get max element of a list of user defined objects using , Program: How to get max element of a list of user defined objects using Comparator? ArrayList;. import java.util.Collections;. import java.util.​Comparator;. I have implemented function that gets average weight value using Java 8 features: public float getAvgChangedWeight() { return (float); } Could you please help improve it because I don't think that casting to double is a good idea.