在此示例中,在没有 new() 的情况下,对实例方法的方法引用如何工作?
How a Method Reference to an instance method is working without new() in this example?
在Collectors.groupingBy(Student::getGrade,....); getGrade() 方法在没有 new 关键字的情况下使用。方法 getGrade 不是静态方法。没有 new
它是如何工作的
class TestClass {
public static void main(String[] args) {
var ls = Arrays.asList(new Student("S1", Student.Grade.A),
new Student("S2", Student.Grade.A),
new Student("S3", Student.Grade.B),
new Student("S4", Student.Grade.C),
new Student("S5", Student.Grade.F));
var group = ls.stream().filter(student -> student.getGrade() != Student.Grade.F).collect(
Collectors.groupingBy(Student::getGrade, Collectors.mapping(Student::getName, Collectors.toList())));
System.out.println(group);
}
}
Lamda 格式
var group1 = ls.stream().filter(student -> student.getGrade() != Student.Grade.F).collect(
Collectors.groupingBy(student -> student.getGrade(), Collectors.mapping(student -> student.getName(), Collectors.toList())));
In Student class getGrade 方法不是静态方法。
class Student {
public static enum Grade {
A, B, C, D, F
}
private String name;
private Grade grade;
public Student(String name, Grade grade) {
this.name = name;
this.grade = grade;
}
public String toString() {
return name + ":" + grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
方法引用匹配 Function<Student, Grade>
。当编译器查看方法 Student::getGrade
时,它发现该方法采用 零 参数,因此它使用传递给 apply
的 Student
对象作为隐含的第一个参数。 (这适用于抽象方法与引用方法完全匹配的任何功能接口,除了它采用目标类型的附加第一个参数。)在 lambda 形式中,它看起来像这样:
(element) -> element.getGrade()
在Collectors.groupingBy(Student::getGrade,....); getGrade() 方法在没有 new 关键字的情况下使用。方法 getGrade 不是静态方法。没有 new
它是如何工作的class TestClass {
public static void main(String[] args) {
var ls = Arrays.asList(new Student("S1", Student.Grade.A),
new Student("S2", Student.Grade.A),
new Student("S3", Student.Grade.B),
new Student("S4", Student.Grade.C),
new Student("S5", Student.Grade.F));
var group = ls.stream().filter(student -> student.getGrade() != Student.Grade.F).collect(
Collectors.groupingBy(Student::getGrade, Collectors.mapping(Student::getName, Collectors.toList())));
System.out.println(group);
}
}
Lamda 格式
var group1 = ls.stream().filter(student -> student.getGrade() != Student.Grade.F).collect(
Collectors.groupingBy(student -> student.getGrade(), Collectors.mapping(student -> student.getName(), Collectors.toList())));
In Student class getGrade 方法不是静态方法。
class Student {
public static enum Grade {
A, B, C, D, F
}
private String name;
private Grade grade;
public Student(String name, Grade grade) {
this.name = name;
this.grade = grade;
}
public String toString() {
return name + ":" + grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
方法引用匹配 Function<Student, Grade>
。当编译器查看方法 Student::getGrade
时,它发现该方法采用 零 参数,因此它使用传递给 apply
的 Student
对象作为隐含的第一个参数。 (这适用于抽象方法与引用方法完全匹配的任何功能接口,除了它采用目标类型的附加第一个参数。)在 lambda 形式中,它看起来像这样:
(element) -> element.getGrade()