Java8 方法引用

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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值