<code>// 如果一个函数接收一个函数作为参数 或者返回一个函数的返回值 那么该函数就叫做高阶函数\t\t\t\t
/<code>
基本语法
(Type arg1,Type arg2)-> {body}
()->{return 3.1415926};
(s)->System.out.println(s);
例子:给一个List排序
<code>public static void main(String[] args) {
List<string> list = Arrays.asList("aa","cc","bb");
//String::toUpperCase; 方法引用
Function<string> function = String::toUpperCase;
//list排序
Collections.sort(list, new Comparator<string>() {
@Override
public int compare(String o1, String o2) {
//倒叙
//如果第一个参数小于第二个参数,就返回一个负数,如果等于就返回0,如果大于就返回一个正数。
return o2.compareTo(o1);
}
});
/**
* expression 表达式 o2.compareTo(o1)
* statement 语句 return o2.compareTo(o1);
*/
Collections.sort(list,(String o1,String o2)->{
return o2.compareTo(o1);
});
Collections.sort(list,(o1,o2)-> o2.compareTo(o1));
Collections.sort(list,Collections.reverseOrder());
System.out.println(list.toString());
/**
* LAmbda 是一种匿名函数没有声明的方法,没有访问修饰符 返回值声明和名字
* 传递行为,而不仅仅是值
* 提升抽象层次
* API重用
* 基本语法
* (Type arg1,Type arg2)-> {body}
* Demo:()->{return 3.1415926};
* Demo:(s)->System.out.println(s);
* 一个Lambda表达式可以有多个或者0个参数
* 所有的参数在()内 空括号元素为空
*
*/
Test05 test05 = new Test05();
System.out.println(test05.compute(1,value->{return value*2;}));
System.out.println(test05.compute(1,value-> value + 2));
}
/**
* Function/<string>/<string>/<string>/<code>
* R apply(T t);
* T是接收 R是返回值
*/
public int compute(int a,Function<integer> func){
return func.apply(a);
}/<integer>
/**
* Lambda 是一种匿名函数没有声明的方法,没有访问修饰符 返回值声明和名字
* 传递行为,而不仅仅是值
* 提升抽象层次
* API重用
* 一个Lambda表达式可以有多个或者0个参数
* 所有的参数在()内 空括号元素为空
*/
欢迎大家相互交流,相互学习
閱讀更多 爪哇程序猿 的文章