Supplier接口练习之获取最大值

public class SupplierTest {
    public static void main(String[] args) {
        //定义数组
        int[] a = {10,20,33,12,99};

        //调用方法获取最大值
        int MaxValue = getMax(()->{
            int Max = a[0];
            for (int i = 1; i<a.length;i++){
                if (a[i] > Max){
                    Max = a[i];
                }
            }
            return Max;
        });

        System.out.println("最大值是:"+MaxValue);
    }
    private static int getMax(Supplier<Integer> sup){
        return sup.get();
    }
}

 

posted @ 2020-06-06 18:57  硬盘红了  阅读(135)  评论(0编辑  收藏  举报