Question:使用Supplier接口作为方法参数类型,通过lambda表达式求出int数组中的最大数
public class DemoTest{
public ststic int getmax(Supplier <Integer> sup){
return sup.get();
}
public ststic void main(String [] args){
int [] arr=new [5];
int value=getmax(()->{
int max=arr[0];
for(int i:arr){
if(max<arr[i]){
max=arr[i];
}
}
return max;
});
System.out.println("数组中元素的最大值是"+value);
}
}

本文介绍了一个使用Java 8 Supplier接口和Lambda表达式来找出整型数组中最大值的方法。通过将Lambda表达式作为参数传递给getmax方法,实现了灵活的功能实现。示例代码展示了如何在main方法中调用此功能,并打印出数组中元素的最大值。
1564





