比较法
public class FunctionSample {
public static void main(String[] args) {
int[] arr = {1,2,13,14,18,101};
Function<int[],Integer> function = (l)->{
int num = 0;
for (int i = 0; i < l.length-1; i++) {
if (l[i]>l[i+1]) {
num = l[i];
}else{
num = l[i+1];
}
}
return num;
};
Integer num = function.apply(arr);
System.out.println("集合中的数据:" + Arrays.toString(arr));
System.out.println("List集合中最大值是:"+num);
}
}
转换成流排序法
public class FunctionSample {
public static void main(String[] args) {
int[] arr = {1,2,13,14,18,101};
Function<int[],Integer> function = (l)->{
Stream<Integer> stream = Arrays.stream(l).boxed();
Optional<Integer> first = stream.sorted((a, b) -> b - a).findFirst();
return first.get();
};
Integer num = function.apply(arr);
System.out.println("集合中的数据:" + Arrays.toString(arr));
System.out.println("List集合中最大值是:"+num);
}
}
版权声明:本文为aa364837061原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。