我上课像:
public class Test {
private String Fname;
private String Lname;
private String Age;
// getters, setters, constructor, toString, equals, hashCode, and so on
}
以及一个List<Test> testList
充满Test
元素的列表。
如何获得age
使用Java 8的最小值和最大值?
为简化起见,您可能应该增加年龄Integer
或int
代替St,但由于您的问题是关于String age
此答案的,因此将取决于String
类型。
假设String age
持有字符串代表整数的范围值,你可以简单地把它映射到IntStream
和使用它的IntSummaryStatistics
像
IntSummaryStatistics summaryStatistics = testList.stream()
.map(Test::getAge)
.mapToInt(Integer::parseInt)
.summaryStatistics();
int max = summaryStatistics.getMax();
int min = summaryStatistics.getMin();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句