Java 8 方法引用
There are four kinds of method references:
Kind | Example |
---|---|
Reference to a static method | ContainingClass::staticMethodName |
Reference to an instance method of a particular object | containingObject::instanceMethodName |
Reference to an instance method of an arbitrary object of a particular type | ContainingType::methodName |
Reference to a constructor | ClassName::new |
方法引用通过方法的名字来指向一个方法。
方法引用可以使语言的构造更紧凑简洁,减少冗余代码。
方法引用使用一对冒号 :: 。
下面,我们定义了 4 个方法作为例子来区分 Java 中 4 种不同方法的引用。
Person
```
import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.List;
public class Person {
@FunctionalInterface
public interface Supplier<T> {
T get();
}
public enum Sex {
MALE, FEMALE
}
String name;
LocalDate birthday;
Sex gender;
String emailAddress;
public Person() {
super();
}
Person(String nameArg, LocalDate birthdayArg, Sex genderArg, String emailArg) {
name = nameArg;
birthday = birthdayArg;
gender = genderArg;
emailAddress = emailArg;
}
public static Person create(Supplier<Person> supplier) {
return supplier.get();
}
public int getAge() {
return birthday.until(IsoChronology.INSTANCE.dateNow()).getYears();
}
public void printPerson() {
System.out.println(name + ", " + this.getAge());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public Sex getGender() {
return gender;
}
public void setGender(Sex gender) {
this.gender = gender;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public static int compareByAge(Person a, Person b) {
return a.birthday.compareTo(b.birthday);
}
public static List<Person> createRoster() {
List<Person> roster = new ArrayList<>();
roster.add(new Person("Fred", IsoChronology.INSTANCE.date(1980, 6, 20), Person.Sex.MALE, "fred@example.com"));
roster.add(new Person("Jane", IsoChronology.INSTANCE.date(1990, 7, 15), Person.Sex.FEMALE, "jane@example.com"));
roster.add(new Person("George", IsoChronology.INSTANCE.date(1991, 8, 13), Person.Sex.MALE, "george@example.com"));
roster.add(new Person("Bob", IsoChronology.INSTANCE.date(2000, 9, 12), Person.Sex.MALE, "bob@example.com"));
return roster;
}
@Override
public String toString() {
return "[" + name + ", " + birthday + "]";
}
}
</div>
```java
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
public class ComparisonProvider {
public int compareByName(Person a, Person b) {
return a.getName().compareTo(b.getName());
}
public static int compareByAge(Person a, Person b) {
return a.getBirthday().compareTo(b.getBirthday());
}
public static void main(String[] args) {
List<Person> roster = Person.createRoster();
Person[] rosterAsArray = roster.toArray(new Person[roster.size()]);
// 特定对象的实例方法引用: containingObject::instanceMethodName
ComparisonProvider myComparisonProvider = new ComparisonProvider();
System.out.println("排序前:" + Arrays.toString(rosterAsArray));
Arrays.sort(rosterAsArray, myComparisonProvider::compareByName);
System.out.println("排序后:" + Arrays.toString(rosterAsArray));
// 静态方法引用:ContainingClass::staticMethodName
System.out.println("排序前:" + Arrays.toString(rosterAsArray));
Arrays.sort(rosterAsArray, ComparisonProvider::compareByAge);
System.out.println("排序后:" + Arrays.toString(rosterAsArray));
// 特定类型的任意对象的实例方法的引用: ContainingType::methodName
String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" };
System.out.println("排序前:" + Arrays.toString(stringArray));
Arrays.sort(stringArray, String::compareToIgnoreCase);
System.out.println("排序后:" + Arrays.toString(stringArray));
// 构造器引用: ClassName::new
Person newPerson = Person.create(Person::new);
newPerson.setName("xiaocao");
newPerson.setBirthday(LocalDate.of(1995, 8, 20));
System.out.println(newPerson);
}
}
点击查看结果
```
排序前:[[Fred, 1980-06-20], [Jane, 1990-07-15], [George, 1991-08-13], [Bob, 2000-09-12]]
排序后:[[Bob, 2000-09-12], [Fred, 1980-06-20], [George, 1991-08-13], [Jane, 1990-07-15]]
排序前:[[Bob, 2000-09-12], [Fred, 1980-06-20], [George, 1991-08-13], [Jane, 1990-07-15]]
排序后:[[Fred, 1980-06-20], [Jane, 1990-07-15], [George, 1991-08-13], [Bob, 2000-09-12]]
排序前:[Barbara, James, Mary, John, Patricia, Robert, Michael, Linda]
排序后:[Barbara, James, John, Linda, Mary, Michael, Patricia, Robert]
[xiaocao, 1995-08-20]
```
参考链接