美文网首页程序员
java8新特性之方法引入

java8新特性之方法引入

作者: 冒险小A | 来源:发表于2018-03-25 20:36 被阅读0次

    我们将一个如下一个lambda表达式再简化

    Array.sort(arr,(x,y) -> Integer.compare(x,y));
    

    简化为

    Array.sort(arr,Integer :: compare);
    

    这种特性就叫做方法引用(Method Reference)。


    方法引入的标准形式为
    类名::静态方法
    对象::方法
    对象::静态方法
    
    代码示例:
    public void test(){
        List<Integer> l = Arrays.asList(2,4,6,8,10);
        arr.forEach(System.out :: println);
    }

    相关文章

      网友评论

        本文标题:java8新特性之方法引入

        本文链接:https://www.haomeiwen.com/subject/cnskcftx.html