https://www.cnblogs.com/xiaoxi/p/7099667.html
/src/method_reference/Person.java
package method_reference; import java.time.LocalDate; public class Person { public Person(String name, LocalDate birthday){ this.name = name; this.birthday = birthday; } String name; LocalDate birthday; public LocalDate getBirthday(){ return birthday; } public static int compareByAge(Person a, Person b){ return a.birthday.compareTo(b.birthday); } @Override public String toString(){ return this.name; } }
/src/method_reference/testMethodReference.java
package method_reference; import java.time.LocalDate; import java.util.Arrays; import java.util.Comparator; import org.junit.Test; import method_reference.Person; public class testMethodReference { @Test public void test(){ Person[] pArr = new Person[]{ new Person("003", LocalDate.of(2016,9,1)), new Person("001", LocalDate.of(2016,2,1)), new Person("002", LocalDate.of(2016,3,1)), new Person("004", LocalDate.of(2016,12,1)) }; // //使用匿名类 // Arrays.sort(pArr, new Comparator<Person>() { // @Override // public int compare(Person o1, Person o2) { // return o1.getBirthday().compareTo(o2.getBirthday()); // } // }); // //使用lambda表达式 未调用已存在的方法 // Arrays.sort(pArr, (Person o1, Person o2) -> { // return o1.getBirthday().compareTo(o2.getBirthday()); // }); //使用方法引用,引用的是类的静态方法 Arrays.sort(pArr, Person::compareByAge); System.out.println(Arrays.asList(pArr)); } }