概述
- supplier接口生产型接口
- 指定为什么类型,那么get就会生产什么类型的对象数据
- get是接口中的方法
案例(一)
import java.util.function.Supplier;
public class SupplierInterface_lianxi {
public static String getString(Supplier<String> sup){
return sup.get();
}
public static void main(String[] args) {
String s = getString(()->"胡歌");
System.out.println(s);
}
}
案例(二)
import java.util.function.Supplier;
public class SupplierInterface_MaxNumber {
public static int getMax(Supplier<Integer> sup){
return sup.get();
}
public static void main(String[] args) {
int[] arr = {85,63,4,0,7};
// System.out.println(Arrays.toString(arr));
int maxNumber = getMax(()->{
int max =arr[0];
for (int i=0;i<arr.length;i++){
if (max < arr[i]){
max =arr[i];
}
}
return max;
});
System.out.println(maxNumber);
int maxValue = getMax(()->{
int max = arr[0];
for (int i:arr){
if (max < i){
max = i;
}
}
return max;
});
System.out.println(maxValue);
}
}