Lambda Expressions In Android ( Part II )


In Part I, I wrote the basics of lambda expression. I am going to write a little about what other methods that can be added to the interface definition (functional interfaces ) and method reference feature of Java 8.

Lambda expression is for interface with only one defined method. If there is other methods to be added on that interface, it will be default and static method definition and implementation only. Look at this link to know more. To illustrate this with example, let us take an interface

public interface IPrintStore {
    void  printName(String s);
}

Now we can easily write a lambda expression for this as

IPrintStore iprintStore = (s) ->
     System.out.println(String.format("You need to print this name =%s", s));
iprintStore.printName("Lambda");

We can only add and implement methods with modifiers default and static as in following example

public interface IPrintStore {
    void printName(String s);
    default void storeName(String s) {
        System.out.println(String.format("Store this %s to database", s));
    }
    public static String addExtraName(String s) {
        String value = s.concat(" :This is extra");
        return value;
    }
}

And use in the following manner

IPrintStore iprintStore = (s) ->
    System.out.println(String.format("You need to print this name =%s", s));
iprintStore.printName("Lambda");
iprintStore.storeName("Lambda");

This is all for functional interfaces in Java 8 and now let us focus on method reference feature of Java 8. As mentioned in Part I, lambda expression is only concerned with method signature ( arguments ) and method implementation ( code block )  of interface having a single method implementation. Method reference also works in similar manner and just like lambda , it is also concerned with arguments, code block and return type of a function.

 

screen-shot-2016-09-09-at-2-00-12-pm

Let’s take a simple analogy ( please don’t jumble up this example with classes and objects ). In order to be a mail man, a person needs to have a hair and a pair of shoes. So, all the person who has a hair and a pair of shoes can be treated as a mailman and those person would readily do the job of mailman.

Screen Shot 2016-09-09 at 4.33.30 PM.png

Now let us move this analogy, to our interface and it’s implementation. Let us create a simple interface named ICombine as

interface ICombine {
    void combine(int age, String name);
}

 

Let us analyze the method definition and signature or to say let us define the criteria which determines whether a function can be treated as object of ICombine

screen-shot-2016-09-15-at-11-12-53-am

 

So, just like mailman analogy, any instance method or static method having same return type or arguments as that of ICombine interface method, it can be treated as objects of ICombine.  So our criteria is a function should have void return type and should take two arguments where first argument must be of type int and second argument must be of type String. So, any method matching this criteria can be taken as ICombine’s object. Take a look at following illustration for it

Screen Shot 2016-09-15 at 11.00.08 AM.png

 

Let us now construct a class having a public and a public static method as

public class MethodReferenceDemo {
    public void methodReferenceInstance(int age, String someName) {
        System.out.println("methodReferenceInstance -----");
	 String value = someName.concat(" age = ").concat(String.valueOf(age));
	 System.out.println("combined value :: " + value);
    }

    public static void methodReferenceStatic(int someInt, String someString) {
        System.out.println("methodReferenceStatic -----");
	String value = someString.concat(" age =
        ").concat(String.valueOf(someInt));
	System.out.println("combined value :: " + value);
    }
}

Here methods methodReferenceInstance and methodReferenceStatic both have same arguments and return type as that of ICombine’s combine method. So, method reference feature of Java 8, can be used to treat the two methods as ICombine’s object in following manner

ICombine combine;
// without method reference
combine = new ICombine() {
    @Override
    public void combine(int age, String name) {
        System.out.println("without method reference---------");
	String value = name.concat(" age = ").concat(String.valueOf(age));
	System.out.println("combined value :: " + value);
    }
};
combine.combine(40, "Middle age guy");
// with method reference
System.out.println("with method reference-----------");
MethodReferenceDemo demo = new MethodReferenceDemo();
combine = demo::methodReferenceInstance;
combine.combine(100, "Old Guy");
combine = MethodReferenceDemo::methodReferenceStatic;
combine.combine(25, "Young Guy");

So, this is all, in method reference feature of Java 8. Hope , you all clearly understood on how it can be used. Look upon these github samples to know more about it.  In part III, I will simply talk about what changes are to be made in Android , for these features to be used.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s