I have class like:
public class Test {
private String Fname;
private String Lname;
private String Age;
// getters, setters, constructor, toString, equals, hashCode, and so on
}
and a list like List<Test> testList
filled with Test
elements.
How can I get minimum and maximum value of age
using Java 8?
To simplify things you should probably make your age Integer
or int
instead of Sting, but since your question is about String age
this answer will be based on String
type.
Assuming that String age
holds String representing value in integer range you could simply map it to IntStream
and use its IntSummaryStatistics
like
IntSummaryStatistics summaryStatistics = testList.stream()
.map(Test::getAge)
.mapToInt(Integer::parseInt)
.summaryStatistics();
int max = summaryStatistics.getMax();
int min = summaryStatistics.getMin();