Easy way to keep how to write Lambda expressions in mind

We use Java 7 for our codes and decided to use Java 8. With this passing, i see that we can use functional programming advantages and i started to learn Lambda expressions. If you are decided to write java lambda expressions in your code, like me, you can confuse about the syntax. Lambda expression syntax is different from usual java code form. Code below is an example;

IHeyLambda<Integer> factorial = (n) -> {
int result = 1;
for (int i = 1; i <= n; i++)
result = i * result;
System.out.println("result:"+result);
return result;
};

Lambda expressions basiccally are implementation of a method of an interface without implement in a class. But in this assay, I will not explain what is lambda expression, i will try to explain how you can get used to write the code in 9 step, a simple logic actually. Of course, you will memorize the way of the write it day after day, but at the beginning, i use this method to keep how to write Lambda expressions in mind.

Lets get start.Our functional interface is

@FunctionalInterface
public interface IHeyLambda {
void sayHelloAndSum(int a, int b);
}

1- Write code normal way;

IHeyLambda heyLambda = new IHeyLambda() {
@Override
public void sayHelloAndSum(int a, int b) {
System.out.println("Hello world Lambda, sum+"+(a+b));
}
};

2-Clean

IHeyLambda heyLambda = public void sayHelloAndSum(int a, int b) {
System.out.println("Hello world Lambda, sum+"+(a+b));
};

We want to use only yhe method for functional programming.

3- Delete accessibility keywords;

IHeyLambda heyLambda = void sayHelloAndSum(int a, int b) {
System.out.println(“Hello world Lambda, sum+”+(a+b));
}

public, private or protected keywords are used for isolation by other classes and determine the accessibility of method. We are not need to use it because the method will used from wherever is called.

4- Delete method name;

IHeyLambda heyLambda = void (int a, int b) {
System.out.println(“Hello world Lambda, sum+”+(a+b));
}

Functional interface has already only one method.It is not necessary to indicate the name.

5-Delete return type;

IHeyLambda heyLambda = (int a, int b) {
System.out.println(“Hello world Lambda, sum+”+(a+b));
}

We already define the return type in method of functional interface.

6- Delete parameter types;

IHeyLambda heyLambda = (a, b) {
System.out.println(“Hello world Lambda, sum:”+(a+b));
}

We are implementing a method not write it again. Parameter types are already defined at functional interface.

7- delete curly brackets but in one condition

IHeyLambda heyLambda = (a,b) System.out.println(“Hello world Lambda, sum:”+(a+b));

If lambda expression is only contains one line you can delete curly brackets.

Actually i don’t like this and i always use curly brackets. I am thinking curly brackets are increase the readability of a code.

8- Delete brackets if there is only one input

IHeyLambda heyLambda = a System.out.println(“Hello world Lambda, input:”+a);

9- Finally do not forget to put the strawberry to top of the cake. Add right arrow to assign lambda expression as a method .

IHeyLambda heyLambda = (a,b) -> System.out.println(“Hello world Lambda, sum:”+(a+b));

Or

IHeyLambda heyLambda = a -> System.out.println(“Hello world Lambda, input:”+a);

I hope you’ve found this assay helpful.

java and angular developer, google cloud, reader, traveller

java and angular developer, google cloud, reader, traveller