Java 8 - Method references

We'll add another field to our simple student management application.
LocalDate birthday;
Now the constructor of the class would look like this.
Person(){}
Person(String name, String emailAddress, int age, Sex gender,LocalDate birthday) {
        this.name = name;
        this.emailAddress = emailAddress;
        this.age = age;
        this.gender = gender;
        this.birthday = birthday;
 }

We'll modify our student database with the new field.
List<Person> peeps=new ArrayList<>();
LocalDate today=LocalDate.now();
peeps.add(new Person("Hasitha","hasitha@gmail.com",23,Sex.MALE,today));
peeps.add(new Person("Nadishan","nadishan@gmail.com",25,Sex.MALE,today.plus(1, ChronoUnit.DAYS)));
peeps.add(new Person("Jayasundara","jayasundara@gmail.com",18,Sex.MALE,today.minusDays(2)));

Somebody may tell you "Hey can you sort the students according to their birthdays?".Of course we can.The solution is simple.
peeps.sort((Person p1,Person p2)->{
      return p1.birthday.compareTo(p2.birthday);
});
peeps.forEach(p->p.printPerson());

The output is,
Jayasundara,MALE,jayasundara@gmail.com,18,2017-01-31
Hasitha,MALE,hasitha@gmail.com,23,2017-02-02
Nadishan,MALE,nadishan@gmail.com,25,2017-02-03

Note that you have to change the printPerson() function little bit in order to print the birthday of the student.As you can see the students are now arranged according to the birthday.Alright...!!!is there any other way we can get the same result?First we'll generalize the compare method as follows.
public static int compareByAge(Person person1, Person person2) {
        return person1.birthday.compareTo(person2.birthday);
}
Now as we want to invoke an existing method we can just use the method reference and simplify our lambda expression as follows.
peeps.sort(Person::compareByAge);
peeps.forEach(p->p.printPerson());
Awesome isn't it?Pay attention to the notation carefully.There are four kinds of method references.
  • Reference to a static method
  • Reference to an instance method of a particular object
  • Reference to an instance method of an arbitrary object of a particular type
  • Reference to a constructor
Let's take one at a time and discuss.The practical we've done so far is related to the static method reference.Look at the following class.

public class AddName {
    String add(String name){
        return name+" is a good boy";
    }
    public static void main(String[] args) {
        String []names={"Hasitha","Nadishan","Jayasundara"};
        AddName addName=new AddName();
        Arrays.asList(names).stream().map(name->addName.add(name)).forEach(newName->System.out.println(newName));
        Arrays.asList(names).stream().map(addName::add).forEach(System.out::println);
    }
}

<>/div>
As illustrated in the AddName class here the method reference is related to an object of AddName class.And you can see how the print process is done.System.out::println This is an instance of the usage of arbitrary object of a particular type as a method reference.



Comments