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

Edit

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:

Output:

2. Minimum

Edit

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:

Output:

Example data used in this article

Edit

User.java:

Alternative titles

  1. Java 8 - maximum and minimum value in List of custom objects
1
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