Thursday, March 13, 2014

Method References :: in Java 8

In this we'll explore method reference, how to use use and its uses with examples. This is continuation of the previous lambda posts.

A lambda expression defines an anonymous method with a functional interface as the target type. Instead of defining an anonymous method, existing methods with a name can be invoked using method references.

Four variations of method references 


The following method invocation has a lambda expression as a method argument.
Arrays.sort(str, (String s1, String s2) -> {return (s1.length() - s2.length());});

The lambda expression can be replaced with a method references as follows:
Arrays.sort(str, LambdaReferenceExample::compareByLength);

The delimiter (::) can be used for a method reference. The method compareByLength is a static method in the LambdaReferenceExample class.






For non-static methods, method references can be used with instances of a particular object.


The method reference doesn't even have to be an instance method of the object in which it is used. The method reference can be an instance method of any arbitrary class object. For example, a String List can be sorted using the compareTo method from the String class with a method reference.




If there is a method whose signature matches the signature of the method of a functional interface,then you can use Class::method (or something similar), rather than an explicit lambda.

DoubleUnaryOperator functional interface has method whose signature matches to Math functions such as cos, log etc. so in this case we can use method reference.

BinaryOperator functional interface has method whose signature matches to Math functions such as min, max, pow etc. so in this case we can use method reference.
System.out::println is equivalent to x -> System.out.println(x)


Now we'll see the third case.
The last one case is different from these above. The first parameter becomes the target of the method.
String::compareToIgnoreCase is the same as (x, y) -> x.compareToIgnoreCase(y)

Summary :


  • Can use ClassName::staticMethodName or variable::instanceMethodName for lambdas.
  • Another way of saying this is that if the function you want to describe already has a name, you don’t have to write a lambda for it,  but can instead just use the method name.
  • The function must match signature of method in functional interface to which it is assigned.
  • The type is found only from the context.




If you know anyone who has started learning java, why not help them out! Just share this post with them. 
Thanks for studying today!...

1 comment:

  1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training in India. or learn thru Java Online Training in India Students. or learn thru JavaScript Online Training in India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

    ReplyDelete