I was following the tutorial here:
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
QUESTION: Am I to understand that -> and :: are the same?
Notice that the interface Comparator is a functional interface. Therefore, you could use a lambda expression instead of defining and then creating a new instance of a class that implements Comparator:
Arrays.sort(rosterAsArray,
(Person a, Person b) -> {
return a.getBirthday().compareTo(b.getBirthday());
}
);
However, this method to compare the birth dates of two Person instances already exists as Person.compareByAge. You can invoke this method instead in the body of the lambda expression:
Arrays.sort(rosterAsArray,
(a, b) -> Person.compareByAge(a, b)
);
Because this lambda expression invokes an existing method, you can use a method reference instead of a lambda expression:
Arrays.sort(rosterAsArray, Person::compareByAge);
The method reference Person::compareByAge is semantically the same as the lambda expression (a, b) -> Person.compareByAge(a, b). Each has the following characteristics:
Its formal parameter list is copied from Comparator<Person>.compare, which is (Person, Person).
Its body calls the method Person.compareByAge.
I am posting the codes from the Tute for your convenience:
Here is the Person class, which looks like the Model to me (something I picked up reading about MVC and MVP and AM-MVC, all very confusing). I can see my collection of classes expanding by a factor of 3.
QUESTION: Can you write this code using a lambda to replace the method reference please?