Lambda Expression in Java 8
Lambda Expression opens the world of functional programming in java. Let's try to understand when we need functional programming.
Let's take a scenario where we need to run a functionality. So as per java7 we need to create a class and object and then finally we can do that functionality. As you can see there are a lot of other things we need to do before writing the actual business logic. That is how object oriented programming works.
But if we can do the same things without writing all such coding. Just to focus on business logic.Those things we can achieve using functional programming. Let's see the below example.
List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API","Java"); Predicate<String> startsWithJ = (n) -> n.startsWith("J"); features.stream().filter(startsWithJ). forEach((n) -> System.out.print("\nName, which starts with 'J' : " + n));
The above example is to check whether a string starts with 'j' or not. If we need to do this as per Java7, we need to create a method and then we will call this method by object of that class.But in case of functional programming it is very easy to implement the same.We can use this predicate bold line code directly without creating a class or any object.So basically functionally programming saves our space and time.we need not to create a class and then object to access such functionality so it will save our memory area also.The code is less as compared to old code, so it is fast also.
Parameter -> expression body
Following are the important characteristics of a lambda expression.
No need to declare the type of a parameter. This job is handled by the compiler.
No need to declare a single parameter in parenthesis. If more than one parameter, parentheses are required.
No need to use curly braces in the expression body if the body contains a single statement.
No return type – The java 8 compiler is able to infer the return type by checking the code. You need not to mention it explicitly.
Example 1: Java Lambda Expression with no parameter
@FunctionalInterface interface FunctionalInterfaceJava { //A method with no parameter public String Hello(); } public class Example { public static void main(String args[]) { // lambda expression FunctionalInterfaceJava output = () -> { return "Hello"; }; System.out.println(output.Hello()); } }
Output:
Hello
Example 2: Java Lambda Expression with single parameter
@FunctionalInterface interface FunctionalInterfaceJava { //A method with single parameter public int incrementByvalue(int a); } public class Example { public static void main(String args[]) { // lambda expression with single parameter num FunctionalInterfaceJava f = (number) -> number+5; System.out.println(f.incrementByvalue(45)); } }
Output:
50
Example 3: Java Lambda Expression with Multiple Parameters
interface FunctionalInterfaceJava { public String strconcat(String a, String b); } public class Example { public static void main(String args[]) { // lambda expression with multiple arguments FunctionalInterfaceJava st = (str1, str2) -> str1 + str2; System.out.println("Result: "+st. strconcat ("Hello ", "Ram")); } }
Output:
Result: Hello Ram
No comments: