Java: Function Reference

Source: Internet
Author: User

You can create an anonymous method by lambda expression. But most of the time, this method may already exist, and you can use a function reference. This will make the code structure clearer.


We know that the greedy algorithm inside, a common step is to sort. Suppose you have the following backpack class:

Class package{    private int weight;    Ignore setter and Getter}

We have a backpack array package[] packages, if you want to sort it:

Arrays.sort (packages,          new comparator<package> () {public          int compare (package A,package b) {         return Integer.valueof (A.getweight ()). CompareTo (Integer.valueof (B.getweight ()));}  );

A friend familiar with lambda expressions sees that comparator is a function interface. In this way, you can instead:

Arrays.sort (Packages,     (package a,package b)->{       return integer.valueof (A.getweight ()). CompareTo ( Integer.valueof (B.getweight ());});

Now, let's imagine if the package class has a static method like this:

public static int Compagew (Package A,package b) {   return integer.valueof (a.weight). ComPareTo (Integer.valueof ( b.weight));}


At this point, you can change the body part of the lambda expression to:

Arrays.sort (packages, (A, B), Person.comparew (A, A,      b));


Because a lambda expression refers to a function that already exists, we can use a function reference.

Arrays.sort (Packages,person::caomparew);


The previous example is a reference to a static function . JAVA8 also supports reference to a specific type instance method , a reference to an arbitrary object function of one type , and a reference to the constructor .


A reference to a particular object instance method is also simple, assuming that the previous Comparew method is not static.

We can use,

Package P = new Package (); Arrays.sort (packages,                p::comparew);


A reference to a specific type instance method, such as the

String[] Stringarray = {"Barbara", "James", "Mary", "John",    "Patricia", "Robert", "Michael", "Linda"}; Arrays.sort (Stringarray, string::comparetoignorecase);


A reference to the constructor,

This example from the Oracle tutorial is to complete the replication between the two collections.

public static <t, SOURCE extends Collection<t>, DEST extends collection<t>> DEST    transferelements (        SOURCE sourcecollection,        supplier<dest> collectionfactory) {                DEST result = Collectionfactory.get ();        for (t t:sourcecollection) {            result.add (t);        }        return result;}

Supplier is a function interface, with only get methods to get objects.

Your call might be,

set<person> Rostersetlambda =    transferelements (roster, (), {return new hashset<> ();});
You can use function reference simplification,

set<person> Rosterset = transferelements (roster, hashset<person>::new);


Finally, a brief description of the supplier interface:

This is a functional interface and can therefore are used as the assignment target for a lambda expression or method refere nCE.





Java: Function 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.