Majid Azimi
Majid Azimi

Reputation: 5745

Using method reference instead of multi argument lambda

I'm confused about concept behind "Reference to an Instance Method of an Arbitrary Object of a Particular Type". Oracle documentation has an example about this:

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

Most of the examples I have seen for this kind of method reference is like this: If lambda is like: x -> x.func() then you can write it like ClassOfX::func. The example in documentation says:

The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).

The question is: for any two argument lambda like (a, b) -> a.func(b) the func method must be instance method of first argument and second argument of lambda will be passed as an argument to that method? If we have multiple argument lambda then func method must be instance method of first argument of lambda and other arguments of lambda will be passed to func in the order the appear in lambda? I mean instead of (a, b, c) -> a.func(b, c) we can write ClassOfA::func

I'm sorry for my English. I hope I made the problem clear.

Upvotes: 23

Views: 24894

Answers (4)

Tao Zhang
Tao Zhang

Reputation: 191

According to Joshua Bloch in his "Effective Java" (3rd Edition), there are 5 types of use cases for method reference.

enter image description here

Upvotes: 5

ajb
ajb

Reputation: 31699

SomeClass::func can mean two things, depending on whether func is a static method or an instance method.

(1) If func is a static method, then SomeClass::func is a lambda that just passes all the arguments to the method:

(a, b, c) -> SomeClass.func(a, b, c);

(2) If func is an instance method, then SomeClass::func is a lambda that uses the first argument as the instance, as you thought:

(a, b, c) -> a.func(b, c);

where a has type SomeClass.

EDIT: Sotirios' answer demonstrates yet a different type of method reference: example::method where example is a reference variable (instead of a class name). This means the same as

(a, b) -> example.method(a, b);

or perhaps more accurately

(a, b) -> __someFinalTemporary.method(a, b);

where __someFinalTemporary is assigned to example at the point where the method reference is evaluated, so that if example changes later, the method is still called using the earlier value of example.

[The fourth kind is SomeClass::new which passes the arguments to a constructor. I think that's all of them.]

Upvotes: 33

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280179

Here's a small example demonstrating the behavior of an instance method reference.

public class Example {  
    public static void main(String[] args) throws Exception {
        List<String> strings = new ArrayList<String>();
        Example example = new Example();
        Functional methodRef = example::method;
        methodRef.funct("a string", strings);
        System.out.println("List now contains: " + strings);

        Functional lambda = (String s, List<String> l) -> {
            example.method(s, l);
        };
        lambda.funct("another string", strings);
        System.out.println("List now contains: " + strings);
    }

    interface Functional {
        void funct(String value, List<String> list);
    }

    void method(String value, List<String> toAddTo) {
        System.out.println("adding");
        toAddTo.add(value);
    }
}

It prints

adding
List now contains: [a string]
adding
List now contains: [a string, another string]

The two are more or less equivalent. In the lambda's case, the variable the method is invoked on needs to be effectively final.

Upvotes: 13

Louis Wasserman
Louis Wasserman

Reputation: 198481

I mean instead of (a, b, c) -> a.func(b, c) we can write ClassOfA::func

Yes, that's correct.

Upvotes: 7

Related Questions