求数组元素最大值
使用 Supplier 接口作为方法参数类型, 通过Lambda表达式求出int数组中的最大值。
import java.util.function.Supplier;
public class Demo {
public static void main(String[] args) {
int arr[] = {1, 3, 4, 7};
int maxNum = getMax(() -> {
int max = arr[0];
for (int i : arr) {
if (i > max) {
max = i;
}
}
return max;
});
System.out.println(maxNum);
}
public static int getMax(Supplier<Integer> supplier) {
return supplier.get();
}
}