Lambda Expressions In Android ( Part I )


One of the many  new features introduced in Java 8, this blog post is focused on lambda expression and method reference. Let’s be clear that Android doesn’t support all the new features of Java 8, but only a subset of new features of Java 8.

So, what is lambda expression then? Before, going into nitty gritty of lambda, let us understand it by an analogy. Lambda is simply a short hand notation. Let us take a scenario. There is a mailman who takes a written command. As per the command, the mailman performs certain task. To save time on writing long commands, both command and mailman is now modified to understand short commands. Take a look at following picture

Screen Shot 2016-09-09 at 1.10.29 PM.jpg

Here, previously the long form As Soon As Possible is now reduced into its short form as ASAP. Under the hood, the mailman interprets ASAP as As Soon As Possible and does the task accordingly. So, the one who is writing the command, it will be easier to write short commands.Under the hood, the whole process works same as before.

So, lambda being a short hand notation, makes the coder easier to write down short codes instead of long ones. Lambda expression is used as a short hand notation for anonymous classes with simple implementation i.e. interface definition with only one method. Rather than getting into jargons and definition, let us illustrate this by a simple example

interface IPrint {
		void printName();
}

And to use, this interface, we need to do

IPrint print;
print = new IPrint() {
   @Override
   public void printName() {
       System.out.println("Print without lambda");
   }
};
print.printName();

During this whole process, the most important part is functional implementation of IPrint  i.e  printName() implementation i.e.

public void printName() {
       System.out.println("Print without lambda");
}

This is simply a function, and in a function the most important part are return type, arguments, block of code

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

Lambda expression is typically shorthand notation for arguments, code block and return type. Let us assume, that we are now concerned with only important part and now we want to write what is important only and scrap out unnecessary parts. Now constructing the same function in short hand notation ( without any lambda expression)

public void printName() {
       System.out.println("Print without lambda");
}
//it's short hand notation be
void (){
   System.out.println("Print without lambda");
}
//we know that if any function returns something, we need to add return statement
//and any function that doesn't return anything, no return statement
//so again the short hand notation be like
(){
  System.out.println("Print without lambda");
}

At this point, don’t get confused where the function name went and all those stuff, just assume we are very busy coders and we want only important stuff of any function definition. Let us look into more examples

IMG_20160909_144828.jpg

Lambda expression is only concerned with method signature ( arguments) and method implementation ( code block) , and provides the short hand notation based upon that. So, the shorthand notation, with lambda expression would be now

img_20160909_150002

Remember, lambda expression is only for interface having single method, implementation. And in order to write lambda expression, you need to know the interface and its method, and then write the short hand notation based upon it. So our interface IPrint will now become

IPrint print;
//without lambda
print = new IPrint() {
    @Override
    public void printName() {
        System.out.println("Print without lambda");
    }
};
print.printName();
//with lambda
print = () -> System.out.println("Print with lambda");
// or
print = () -> {
    System.out.println("First print with lambda");
    System.out.println("Second print with lambda");
};

Let’s see other implementation, an interface having a return type

interface IPrint {
    String printName();
}
IPrint print;
//without lambda
print = new IPrint() {
    @Override
    public String printName() {
        String value = "Without lambda";
	System.out.println(value + " -------------");
	return value;
    }
};
//with lambda
print = () -> System.out.println("With lambda");
print = () -> {
    String value = "With lambda again";
    System.out.println(value + " -------");
    return value;
};

Another implementation where an interface has arguments and return type

interface IPrint {
    int printName(int age, String name);
}
IPrint print;
//without lambda
print = new IPrint() {
    @Override
    public int printName(int age, String name) {
        System.out.println("Without lambda--------");
	System.out.println("Your name = " + name);
	System.out.println("Your age = " + age);
	return age * 10;
    }
};
//with lambdas
//notice the arguments name can be any and type definition not required
print = (a, b) -> {
    System.out.println("With lambda--------");
    System.out.println("Your name = " + b);
    System.out.println("Your age = " + a);
    return a * 20;
    };

print = (int age, String name) -> {
    System.out.println("With lambda again --------");
    System.out.println("Your name String value = " + name);
    System.out.println("Your age int value = " + age);
    return age * 30;
    };

Remember there is no performance gain by writing short hand notation. Lambda is simply ease of writing codes and is concerned with short hand form of method signature and method implementationscreen-shot-2016-09-09-at-3-19-18-pm

Here is the github link which contains, all the examples mentioned in this blog post. In part II I will talk  about method reference feature of Java 8 and finally how we can use these features in Android.

Advertisements

One thought on “Lambda Expressions In Android ( Part I )

  1. मेरो लागी कालो अक्षर भैसी बराबर!! तर पक्कै राम्रै होला !!!

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