文章目录
一、目标
- 方法引用的目的
- 了解方法引用的格式
- 了解常用的几种方法引用方式
二、化繁就简,方法引用代替冗余代码
1. 一个栗子
public class DemoMethodImpl {
public static void main(String[] args) {
DemoMethodImpl demo = new DemoMethodImpl();
//调用m1方法,并实现lambda表达式业务代码
/*Integer m1Result = demo.m1(f1 -> {
return Integer.valueOf(f1);
});*/
//利用 方法引用 减少了冗余代码
//通过指定stringToInt方法重写接口的抽象方法,调用接口的抽象方法就是调用该方法
Integer m1Result = demo.m1(demo::stringToInt);
System.out.println(m1Result.getClass() + ",m1Result = " + m1Result);
}
//普通方法,字符串转整数
public Integer stringToInt(String numStr){
return Integer.valueOf(numStr);
}
public Integer m1(Function<String, Integer> f1){
return f1.apply("999");
}
}
==运行结果==
class java.lang.Integer,m1Result = 999
如上所见,通过方法引用的方式,可以省略冗余的重复代码。
2. 方法引用的几种格式
- 运算符号:
::
,有此符号所在的表达式被称为“方法引用” - 应用场景:如果lambda表达式所要实现的功能,已经有其他已存在的方法实现了该功能,则可以使用方法是用
- 常见格式:
instanceName::methodName
对象::方法名className::staticMethodName
类名::静态方法名className::methodName
类名::方法名className::new
类名::new(调用构造方法)TypeName[]::new
调用数组的构造方法
三、几种常见格式的使用示例
1. instanceName::methodName
对象::方法名
public class DemoMethod1 {
public static void main(String[] args) {
int[] intArr = new int[]{1,2,3,4,5};
DemoMethod1 demoMethod1 = new DemoMethod1();
//利用方法引用 代替 lambda表达式
int getmax = demoMethod1.getMaxLambda(demoMethod1::getmax, intArr);
System.out.println("getmax = " + getmax);
}
//求数组中的最大值
public Integer getmax(int[] ints){
int max = ints[0];
for (int anInt : ints) {
if(anInt > max){
max = anInt;
}
}
return max;
}
//
public int getMaxLambda(Function<int[], Integer> f1, int[] intArr){
return f1.apply(intArr);
}
}
==运行结果==
getmax = 5
2. className::staticMethodName
类名::静态方法名
public class DemoMethod2 {
public static void main(String[] args) {
//lambda表达式定义
Supplier<Long> s1 = ()->{
return System.currentTimeMillis();
};
//静态方法引用
Supplier<Long> s2 = System::currentTimeMillis;
Long aLong = s1.get();
System.out.println("aLong = " + aLong);
System.out.println("---------------------");
Long aLong1 = s2.get();
System.out.println("aLong1 = " + aLong1);
}
}
==运行结果==
aLong = 1583745638473
---------------------
aLong1 = 1583745638474
3. className::methodName
类名::方法名
类名调用实例方法这种方式有一个前提:用第一个参数作为方法的调用者
public class DemoMethod3 {
public static void main(String[] args) {
//lambda表达式
Function<String, Integer> f1 = (str)->{
return str.length();
};
//类名::方法名
Function<String, Integer> f2 = String::length;
BiFunction<String, Integer, String> bf1 = String::substring;
String s1 = "hello world";
Integer apply1 = f1.apply(s1);
System.out.println("apply1 = " + apply1);
System.out.println("------------------");
Integer apply2 = f2.apply(s1);
System.out.println("apply2 = " + apply2);
System.out.println("------------------");
String apply3 = bf1.apply(s1, 4);
System.out.println("apply3 = " + apply3);
}
}
==运行结果==
apply1 = 11
------------------
apply2 = 11
------------------
apply3 = o world
4. className::new
类名::new(调用构造方法)
public class DemoMethod4 {
public static void main(String[] args) {
Supplier<Person> s1 = ()->{
return new Person();
};
Supplier<Person> s2 = Person::new;
Person person1 = s1.get();
System.out.println("person1 = " + person1.toString());
Person person2 = s2.get();
System.out.println("person2 = " + person2.toString());
System.out.println("----------------");
BiFunction<String, Integer, Person> bi1 = (name, age)->{
return new Person(name, age);
};
BiFunction<String, Integer, Person> bi2 = Person::new;
Person p3 = bi1.apply("xiaoming", 13);
System.out.println("p3 = " + p3.toString());
Person p4 = bi2.apply("xiaohong", 14);
System.out.println("p4 = " + p4.toString());
}
}
class Person{
private String name;
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
//getting、setting、toString
}
==运行结果==
person1 = Person{name='null', age=null}
person2 = Person{name='null', age=null}
----------------
p3 = Person{name='xiaoming', age=13}
p4 = Person{name='xiaohong', age=14}
5. TypeName[]::new
调用数组的构造方法
public class DemoMethod5 {
public static void main(String[] args) {
Function<Integer, int[]> f1 = (i)->{
return new int[i];
};
Function<Integer, int[]> f2 = int[]::new;
int[] apply = f1.apply(5);
System.out.println("apply = " + apply);
int[] apply1 = f2.apply(5);
System.out.println("apply1 = " + apply1);
}
}