1、使用java.util.Collections工具类,上代码
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
integerList.add(15);
integerList.add(103);
integerList.add(21);
integerList.add(45);
// 最小值
System.out.println("min:" + Collections.min(integerList));
// 最大值
System.out.println("max:" + Collections.max(integerList));
}
2、使用jdk1.8 stream api,
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
integerList.add(15);
integerList.add(103);
integerList.add(21);
integerList.add(45);
// 最小值
Integer min = integerList.stream().reduce(Integer::min).get();
// 最大值
Integer max = integerList.stream().reduce(Integer::max).get();
System.out.println("min:" + min);
System.out.println("max:" + max);
}