A brief analysis of Java 8 new features method Reference

Source: Internet
Author: User
Tags instance method

What is a method reference

We know what lambda expression is and how it is used, so what is Method references? In Oracle Java docs, this says:

They is compact, easy-to-read lambda expressions for methods that already has a name.

In other words, it is used as a lambda expression to define a named method that is already defined.

Understanding Method References

In one example: there is an array with a bunch of person objects in it, and now you want to sort it according to birthdays.

Person class

publicclass Person {    LocalDate birthday;    publicgetBirthday() {        return birthday;    }        publicstaticintcompareByAge(Person a, Person b) {        return a.birthday.compareTo(b.birthday);    }}

General wording

Person[] rosterAsArray = roster.toArray(new Person[roster.size()]);classimplements Comparator<Person> {    publicintcompare(Person a, Person b) {        return a.getBirthday().compareTo(b.getBirthday());    }}Arrays.sortnewPersonAgeComparator());

Lambda Expression 1: Manual comparison of two date

Arrays.sort(rosterAsArray,    (Person a, Person b) -> {        return a.getBirthday().compareTo(b.getBirthday());    });

Analyze This lambda Expression

    • First, it overwrite the Comparator.compare () method.
    • Then, take a look at the parameters of Compare (): int compare (t O1, T O2), its input is two types consistent object, the return value is type int. Corresponds to an expression:
    • Left: (Person A, person B), defines an input parameter, an object of type two.
    • Right: A.getbirthday (). CompareTo (B.getbirthday ()), the output parameter is defined, and the type is int.

We found that for the right side, you do not have to call the Date.compareto () method, any other custom methods can be, only need to meet the input and output parameters consistent.
Exactly, the person class has defined a Comparebyage () method that can be replaced directly. Thus, it can be changed to:

LAMBDA Expression 2: Call a method that has already been defined

Arrays.sort(rosterAsArray,    (Person a, Person b) -> {        return Person.compareByAge(a, b);    });

Further analysis, it can be found that the left (person A, person B) is actually redundant information, because the right person.comparebyage (A, B) method has defined the input and output.

Therefore, the above code can be simplified as follows:

Method reference notation

Arrays.sort(rosterAsArray, Person::compareByAge);
Several types

Method reference are available in the following categories:

    • Reference static method: Classname::staticmethodname
    • Reference Construction Method: Classname::new
    • To reference an instance method on an instance apply: Instancereference::instancemethodname
    • Reference to an instance method reference on a type: Classname::instancemethodname
Link
    • Https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
    • https://www.zhihu.com/question/54665934
    • Https://www.cnblogs.com/JohnTsai/p/5806194.html

A brief analysis of Java 8 new features method Reference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.