1、概念
方法引用实际是对Lambda表达式的一种优化;
适用前提:当已经存在对应的类、对象、方法,则可以使用方法引用优化Lambda表达式
::为方法引用符
2、使用方法引用来简化Lambda表达式
// 使用方法引用简化Lambda表达式
Stream<String> stream1 = Stream.of("1", "2", "3", "4", "5");
Stream<String> stream2 = Stream.of("6", "7", "8", "9");
Stream<String> concat = Stream.concat(stream1, stream2);
// concat.forEach(s -> System.out.println(s));
// 等效于上方的代码
/*
注意:
1.System.out对象是已经存在的
2.println方法也是已经存在的
所以可以使用方法引用来优化Lambda表达式
*/
concat.forEach(System.out::println);
3、通过对象名引用成员方法
语法:对象名::方法名
@FunctionalInterface
public interface PrintString {
public abstract void printString(String s);
}
public class ObjectCiteFunction {
public void printUpperCaseString(String s) {
System.out.println(s.toUpperCase());
}
}
public class Demo10Main {
public static void printString(PrintString p) {
p.printString("Hello_Java");
}
public static void main(String[] args) {
ObjectCiteFunction obj = new ObjectCiteFunction();
/*
对象已经存在
方法已经存在
因此就可以通过对象直接引用方法
*/
printString(obj::printUpperCaseString);
}
}
4、通过类名引用静态成员方法
语法:类名称::成员方法名
public interface Calculate {
public abstract int CalcAbs(int i);
}
public class CalcMain {
public static int method(int i, Calculate c) {
return c.CalcAbs(i);
}
public static void main(String[] args) {
// int num = method(-10, i -> Math.abs(i));
// System.out.println(num);
// 下方代码等价于上方代码
/*
通过类名引用静态成员方法
类已经存在,静态成员方法也已经存在
就可以通过类名直接引用静态成员方法
*/
int num = method(-10, Math::abs);
System.out.println(num);
}
}
5、通过super引用父类的成员方法
语法:super::方法名
@FunctionalInterface
public interface Greet {
public abstract void greet();
}
public class Fu {
public void fu() {
System.out.println("我是父");
}
}
public class Zi extends Fu{
@Override
public void fu() {
System.out.println("我是子类");
}
public void method(Greet g) {
g.greet();
}
public void show() {
// method(() -> super.fu());
// 等同于上方代码
method(super::fu);
}
public static void main(String[] args) {
new Zi().show();
}
}
6、通过this引用本类的成员方法
语法:this::方法名
@FunctionalInterface
public interface Stock {
public abstract void buy();
}
public class JiuCai {
public void buyStock() {
System.out.println("韭菜又买了100手包钢,跌停了");
}
public void method(Stock s) {
s.buy();
}
public void show() {
// method(() -> this.buyStock());
method(this::buyStock);
}
public static void main(String[] args) {
new JiuCai().show();
}
}
7、类的构造器(构造方法)引用
语法:类名称::new
@FunctionalInterface
public interface StudentBuilder {
public abstract Student BuilderStudent(String name);
}
public class Student {
private String name;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
public class Test {
public static Student method(String name, StudentBuilder s) {
return s.BuilderStudent(name);
}
public static void main(String[] args) {
// Student stu = method("马尔扎哈", name -> new Student(name));
Student stu = method("马尔扎哈", Student::new);
System.out.println(stu.getName());
}
}
8、数组的构造器引用
@FunctionalInterface
public interface ArrayBuilder {
public abstract int[] builderArray(int len);
}
import java.util.Arrays;
public class TestMain {
public static int[] createArray(int len, ArrayBuilder a) {
return a.builderArray(len);
}
public static void main(String[] args) {
// int[] arr = createArray(10, len -> new int[len]);
int[] arr = createArray(10, int[]::new);
arr[0] = 10;
System.out.println(Arrays.toString(arr));
}
}