Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...