Given a list of objects, find minimum and maximum values in a list of objects in Kotlin.

1. Using minWith() & maxWith() function

The recommended solution is to find the minimum value in the list of objects is with the minWith() function that accepts a Comparator to compare objects based on a field value. Similarly, to find the maximum value in the list, you can use the maxWith() function.

Download Code

 
Output:

Minimum : Emp(name=Mike, age=10)
Maximum : Emp(name=Vicki, age=15)

2. Using Reduce Operation

Alternatively, you can perform the reduction operation on the list objects using the reduce() function that takes the comparison object.

Download Code

 
Output:

Minimum : Emp(name=Mike, age=10)
Maximum : Emp(name=Vicki, age=15)

That’s all about finding the minimum and maximum values in a list of objects in Kotlin.