Search code examples
javajava-8minimumjava-stream

Java8: select min value from specific field of the objects in a list


Suppose to have a class Obj

class Obj{

  int field;
}

and that you have a list of Obj instances, i.e. List<Obj> lst.

Now, how can I find in Java8 the minimum value of the int fields field from the objects in list lst?


Solution

  • You can also do

    int min = list.stream().mapToInt(Obj::getField).min();