原标题:jdk8新特性之双冒号 :: 用法及详解
jdk8的新特性有很多,最亮眼的当属函数式编程的语法糖,本文主要讲解下双冒号::的用法。
概念
类名::方法名,相当于对这个方法闭包的引用,类似js中的一个function。比如:
Function< String, String> func = String::toUpperCase;
(Function在java.util.function包下,也是jdk8新加入的类,同级目录下有很多函数式编程模型接口,比如Consumer/Predicate/Operator等)
func相当于一个入参和出参都为String的函数,可以直接
func.apply( "abc")
接收一个参数,返回一个结果("ABC")。也可以用于代替下面的Lambda表达式:
List l = Arrays.asList( "a", "b", "c");
l.stream(). map(s -> s.toUpperCase());
l.stream(). map( func);
下面自定义一个函数式接口
publicclassMyConsumer implementsConsumer{
@Override
publicvoidaccept(String s){
System.out.println(s);
}
}
下面这俩种写法等价:
List l = Arrays.asList( "a", "b", "c");
l. forEach( newMyConsumer<>());
l. forEach(s -> System.out.println(s));
但是,这种写法却不行,编译失败:
l.forEach( MyConsumer::accept);
因为MyConsumer的accept方法不是静态的,如果想使用这个方法,需要一个实例,还需要一个入参,共俩个参数。而List.forEach中需要的是consumer类型,相当于s -> {...},只有一个参数。
下面详细分析双冒号使用的各种情况
新建一个类,里面声明四个代表各种情况的方法:
publicclassDoubleColon{
publicstaticvoidprintStr(String str){
System. out.println( "printStr : "+ str);
}
publicvoidtoUpper(){
System. out.println( "toUpper : "+ this.toString());
}
publicvoidtoLower(String str){
System. out.println( "toLower : "+ str);
}
publicinttoInt(String str){
System. out.println( "toInt : "+ str);
return1;
}
}
把它们用::提取为函数,再使用:
Consumer printStrConsumer = DoubleColon::printStr;
printStrConsumer.accept( "printStrConsumer");
Consumer toUpperConsumer = DoubleColon::toUpper;
toUpperConsumer.accept( newDoubleColon());
BiConsumer toLowerConsumer = DoubleColon::toLower;
toLowerConsumer.accept( newDoubleColon(), "toLowerConsumer");
BiFunction toIntFunction = DoubleColon::toInt;
int i = toIntFunction.apply( newDoubleColon(), "toInt");
非静态方法的第一个参数为被调用的对象,后面是入参。静态方法因为jvm已有对象,直接接收入参。
再写一个方法使用提取出来的函数:
publicclassTestBiConsumer{
publicvoidtest(BiConsumer consumer){
System. out.println( "do something ...");
}
}
下面这俩种传入的函数是一样的:
TestBiConsumer obj = newTestBiConsumer();
obj.test( (x,y)->System.out.println( "do something ..."));
obj.test(DoubleColon::toLower);
总结
用::提取的函数,最主要的区别在于静态与非静态方法,非静态方法比静态方法多一个参数,就是被调用的实例。
来源:https://my.oschina.net
/wangbo888/blog/1942354返回搜狐,查看更多
责任编辑: