Languages
[Edit]
EN

Java 8 - find max and min value in List of objects

0 points
Created by:
ParaEagle
724

In this article, we would like to show you how to find maximum and minimum values in a list of custom objects in Java 8.

 

1. Maximum

In this example, we use:

  • stream() - to get a stream of values from the users list,
  • max() method on the stream - to get the maximum value
    • we are passing a custom comparator¬†that specifies¬†the sorting logic when determining the maximum value,
  • orElseThrow()¬†- to throw an exception if no value is received from max() method.

Practical example:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;


public class Example {

    public static void main(String[] args) throws IOException {

        User user1 = new User("Tom", 20);
        User user2 = new User("Kate", 21);
        User user3 = new User("Ann", 22);

        List<User> users = new ArrayList<>();

        users.add(user1);
        users.add(user2);
        users.add(user3);

        User maxAge = users
                .stream()
                .max(Comparator.comparing(User::getAge))
                .orElseThrow(NoSuchElementException::new);

        System.out.println(maxAge);
    }
}

Output:

User{ name='Ann', age=22 }

2. Minimum

In this example, we use:

  • stream() - to get a stream of values from the users list,
  • min() method on the stream - to get the minimum value
    • we are passing a custom comparator¬†that specifies¬†the sorting logic when determining the minimum value.
  • orElseThrow()¬†- to throw an exception if no value is received from min() method.

Practical example:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;


public class Example {

    public static void main(String[] args) throws IOException {

        User user1 = new User("Tom", 20);
        User user2 = new User("Kate", 21);
        User user3 = new User("Ann", 22);

        List<User> users = new ArrayList<>();

        users.add(user1);
        users.add(user2);
        users.add(user3);

        User minAge = users
                .stream()
                .min(Comparator.comparing(User::getAge))
                .orElseThrow(NoSuchElementException::new);

        System.out.println(minAge);
    }
}

Output:

User{ name='Tom', age=20 }

Example data used in this article

User.java:

public class User {
    private final String name;
    private final int age;

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "User{ " +
                "name='" + name + '\'' +
                ", age=" + age +
                " }";
    }
}

Alternative titles

  1. Java 8 - maximum and minimum value in List of custom objects
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join