System.out.println("toLower : " + str);
}
public int toInt(String str){
System.out.println("toInt : " + str);
return 1;
}
}
复制代码
用::提取的函数,最主要的区别在于静态与非静态方法,非静态方法比静态方法多一个参数,就是被调用的实例。
// 使用双冒号::来构造非静态函数引用
String content = “Hello JDK8”;
// public String substring(int beginIndex)
// 写法一: 对象::非静态方法
Function<Integer, String> func = content::substring;
String result = func.apply(1);
System.out.println(result);
// 写法二:
IntFunction intFunc = content::substring;
result = intFunc.apply(1);
System.out.println(result);
// 写法三: String::非静态方法
BiFunction<String,Integer,String> lala = String::substri
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
ng;
String s = lala.apply(content, 1);
System.out.println(s);
// public String toUpperCase()
// 写法一: 函数引用也是一种函数式接口,所以也可以将函数引用作为方法的参数
Function<String, String> func2 = String::toUpperCase;
result = func2.apply(“lalala”);
System.out.println(result);
// 写法二: 可以改写成Supplier: 入参void, 返回值String
Supplier supplier = “alalal”::toUpperCase;
result = supplier.get();
System.out.println(result);
复制代码
数组引用
// 传统Lambda实现
IntFunction<int[]> functio