import java.util.function.Supplier; public class SupplierTest { public static void main(String[] args) { // 定义一个数组 int[] arr = {42, 55, 78, 54, 4, 65, 99}; // 定义一个变量存放数组中最大值 int maxrsult = getMax(() -> { //代码块,求数组的最大值 int max = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } // 返回结果 return max; }); // 打印输出 System.out.println(maxrsult); } // 定义方法,用来返回最大值,使用Supplier函数接口 private static int getMax(Supplier<Integer> sup) { return sup.get(); } }