Is it true to say that in a Stream
in Java 8, you can only use method references that take a single argument (if you disallow wrapping the method reference with a method call)?
I assume so because in a stream at any time you are processing a single item.
Therefore:
Something::new
(must refer to a single arg constructor)this::doSomething
(must take a single arg)Something::doSomething
(must take a single arg)...when used in a Stream
. Is this rule always true?
No, it's not. Some of the Stream
methods take functional interfaces having a method with multiple arguments.
For example, Stream
's sorted(Stream<T> Comparator<? super T> comparator)
method, takes a Comparator
, whose method has two arguments.
Here's an example of using a method reference - String::compareTo
- of a method having two arguments:
System.out.println(Stream.of("a","d","c").sorted(String::compareTo).collect(Collectors.toList()));
Stream
's Optional<T> max(Comparator<? super T> comparator)
method is another similar example.
There are four types of methods references:
A method reference to a static method i.e.
Class::staticMethod
--> (args) -> Class.staticMethod(args)
A method reference to an instance method of an object of a particular type. i.e.
ObjectType::instanceMethod
--> (obj, args) -> obj.instanceMethod(args)
A method reference to an instance method of an existing object i.e.
obj::instanceMethod
--> (args) -> obj.instanceMethod(args)
A method reference to a constructor i.e.
ClassName::new
--> (args) -> new ClassName(args)
As you can see with the second example, a given method can take two arguments and still be translated to a method reference, this is true for the case of calling sorted
, min
, max
etc.. of a stream.
credit to Java 8 Method Reference: How to Use it for the examples above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With