函数式接口Supplier的使用

点击查看代码
package it_09;

import java.util.function.Supplier;

public class Demo5 {
    public static void main(String[] args) {
        int[] array ={2,6,9,5,3};
        int result = getMax(() -> {
            int max = 0;
            for (int i = 0; i < array.length; i++) {
                if (array[i] > max) {
                    max = array[i];
                }
            }
            return max;
        });
        System.out.println(result);
    }
    public static int getMax(Supplier<Integer> sup){
       return sup.get();
    }
}
posted @ 2025-05-20 21:54  lfqyj  阅读(23)  评论(0)    收藏  举报