Java8 Method Referrance
Java 8 enables you to pass references of methods or constructors via the :: keyword;
that means you can be further simplified by utilizing static method references;
In those cases, it’s often clearer to refer to the existing method by name.
总之,Java8提供了引用已经存在的方法的能力;
看一个例子:
1 接口IsReferable
// Don't forget Functional interfaces are heart of labmda expression.
interface IsReferable {
public void reference();
}
2 已经存在的commonMethod方法
class Reference {
public static void commonMethod() {
System.out.println("This method is already defined.");
}
}
3 调用
三种实现:
1)匿名类:之前的写法;
2)lambda表达式:很简洁;
3)方法引用:综合利用&更加简洁;
public class Client{
public static void main(String[] args){
//method reference three realization
// 1 Anonymous class.
IsReferable demo = new IsReferable() {
@Override
public void reference() {
Reference.staticMethod();
}
};
// 2 Lambda implementaion.
IsReferable demoOne = () -> Reference.staticMethod();
// 3 Method reference.
IsReferable demoTwo = Reference::staticMethod;
//调用执行
demo.reference();
demoOne.reference();
demoTwo.reference();
}
}
执行结果:
This is Reference's static method.
This is Reference's static method.
This is Reference's static method.
4 方法引用方式汇总
上边例子是引用一个静态方法,方法被引用的语法如下:
1)Reference to a static method : ClassName::staticMethod
静态方法引用:它的语法是Class::static_method
Reference::staticMethod
2)Reference to an instance method of a particular object : instance::method
特定实例对象的方法引用:它的语法是instance::method
new Reference()::commonMethod
3)Reference to an instance method of an arbitrary object of a particular type : ContainingType::methodName
任意对象(属于同一个类)的实例方法引用:它的语法是Class::method
String::compareToIgnoreCase
4)Reference to a constructor : ClassName::new
构造器引用:它的语法是Class::new,或者更一般的Class< T >::new
String::new
示例:
//different type method be referenced by lambda
// 4 引用特定实例方法(非静态方法这样引用)
IsReferable demoThree = new Reference()::commonMethod;
demoThree.reference();
//5 引用任意对象(属于同一个类)的实例方法,主要用于操作集合
//5.1 例1
List<Reference> stringArray = new ArrayList();
stringArray.add(new Reference("Barbara"));
stringArray.add(new Reference("James"));
stringArray.add(new Reference("Mary"));
stringArray.add(new Reference("Weision"));
stringArray.forEach(Reference::getName);
//5.2 例2
String[] strArray = {"c", "d", "e", "John", "f", "g", "a", "b"};
Arrays.sort(strArray, String::compareToIgnoreCase);
for (String s : strArray)
System.out.println("*****" + s + "*****");
//6 引用构造方法
IsReferable demoSix = Reference::new;
demoSix.reference();
5 总结
只要参数返回一致就可方法引用,被引用方法根据其方法类型有几种不用的引用姿势
/**
* 只要参数返回一致就可方法引用
*/
public class Main {
public void mapString() {
Stream<String> arrayStream = Arrays.asList("555", "333", "444", "111", "222", "666").stream();
arrayStream.map(this::mapString);
arrayStream.map((s) -> mapString(s));
}
private String mapString(String str) {
return str + "Hey";
}
}
涉及代码:–>GitHub
参考文献:
[ 1 ] http://java8.in/java-8-method-references/
[ 2 ] https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
[ 3 ] https://winterbe.com/posts/2014/03/16/java-8-tutorial/