zoukankan      html  css  js  c++  java
  • java8 lambda方法引用

    注意引用方法的参数列表与返回值类型要与函数式接口中的抽象方法的参数列表与返回值类型保持一致
    主要有三种语法格式:
     *
     * 对象::实例方法名
     *
     * 类::静态方法名
     *
     * 类::实例方法名

    public class Test05 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            //注意引用方法的参数列表与返回值类型要与函数式接口中的抽象方法的参数列表与返回值类型保持一致
            
            //lambda方法引用        Class::staticMethod
            int[]  arr=new int[]{5,4,3,2,1}; 
            SortArray s=Arrays::sort;
            s.sortArray(arr);
            System.out.println(Arrays.toString(arr));  //[1, 2, 3, 4, 5]
            
            //lambda方法引用        object::instanceMethod
            Print pri=new Print();
            SystemPrint sp=pri::print;
            sp.print(100);     //100
            
            //lambda方法引用        Class::instanceMethod
            String str1="b",str2="a";
            ClassInstance ci=String::compareToIgnoreCase;
            int result=ci.comp(str1, str2);
            System.out.println(result);  //1
        }
    
    }
    
    
    interface SortArray{
        void sortArray(int[] o);
    }
    
    //************************************
    interface SystemPrint{
        void print(int i);
    }
    class Print{
        void print(int i){
            System.out.println(i);
        }
    }
    //***********************************
    interface ClassInstance{
        int comp(String first,String another);
    }
    View Code
  • 相关阅读:
    Median Value
    237. Delete Node in a Linked List
    206. Reverse Linked List
    160. Intersection of Two Linked Lists
    83. Remove Duplicates from Sorted List
    21. Merge Two Sorted Lists
    477. Total Hamming Distance
    421. Maximum XOR of Two Numbers in an Array
    397. Integer Replacement
    318. Maximum Product of Word Lengths
  • 原文地址:https://www.cnblogs.com/xiu68/p/7412858.html
Copyright © 2011-2022 走看看