package com.chunzhi.Test04Supplier;
import java.util.function.Supplier;
/*
练习:求数组元素最大值
使用Supplier接口作为方法参数类型,通过Lambda表达式求出int数组中的最大值
提示:接口的泛型请使用java.lang.Integer类
*/
public class Test02 {
// 定义一个方法,用于获取int类型数组中元素的最大值,方法的参数传递Supplier接口,泛型使用Integer
public static int getMax(Supplier<Integer> sup) {
return sup.get();
}
public static void main(String[] args) {
// 定义一个int类型的数组,并赋值
int[] arr = {1616, 7455, -22, 55524, 9343};
// 调用getMax方法,方法的参数Supplier是一个函数式接口,所以可以传递Lambda表达式
int maxValue = getMax(() -> {
// 获取数组的最大值,并返回
// 定义一个变量,把数组中的第一个元素赋值给该变量,记录数组中元素的最大值
int max = arr[0];
// 遍历数组,获取数组中的其它元素
for (int i : arr) {
// 使用其它的元素和最大值进行比较
if (i > max) {
// 如果i大于max,则替换max作为最大值
max = i;
}
}
// 返回最大值
return max;
});
System.out.println("数组中元素的最大值是:" + maxValue); // 数组中元素的最大值是:55524
}
}