廖雪峰Java16函数式编程-1Lambda表达式-3方法引用

Java8引入了Lambda表达式,可以不必编写FunctionalInterface的实现类,直接写Lambda表达式。除了Lambda表达式,我们还可以直接传入方法引用

方法引用是指:如果某个方法签名和接口恰好一致:

  • 可以直接传入方法引用,格式:类名::方法名

这里的方法签名只看参数类型和返回值

静态方法引用

class SortedBy{
    static int ignoreCase(String s1, String s2){                        <==> interface Comparator<String>{
        return s1.toLowerCase.compareTo(s2.toLowerCase());                    int compare(String s1, String s2);
    }                                                                                 }
}
Arrays.sort(array, SortedBy::ignoreCase);

猜你喜欢

转载自www.cnblogs.com/csj2018/p/11469816.html