Lambda Expression in Java 8

|
| By Webner

A lambda expression is a new feature that has been added to Java 8.
It is a function declared inside a method to which we can pass arguments as well as fetch lambda expressions in the form of return value of a function.

Significance:
This feature is a way to write method at the same place where we need to use it, rather than making effort to create a separate function. This will be helpful only if we need this function once. To use it at different places, the old methodology of declaring a function in the containing class is considered a better way.
Sample expression :
i -> i % 2 == 1
where,
i is the input parameter
i % 2 is the expression

Expression “ i => i % 2 “ can be read like : “input parameter i goes to anonymous function which returns true if the input is odd”.

Implementation :

List list = new ArrayList();
 list.add(23);
 list.add(20);
 list.forEach(number -> System.out.println(number));
 list.stream().filter(n -> n % 2 == 1).forEach(System.out::println);

Output :23
20
23
Description :
In the above example, “ list.stream() “ returns a stream of Integers in a sorted form. Further, “filter()“ method does an intermediate operation that returns a stream of integers consisting of the elements of this stream that matches the given condition. And at the end there is a “forEach“ method which is a terminal operation that performs an action for each element of this stream.

Example :
Print out the list of integers
Solution :

List list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(i - & gt; System.out.print(i));

Output: 12345
Explanation : forEach() is a method that accepts a function as input and calls the function for each value of the collection.
i -> System.out.print(i), is a lambda expression, this defines an anonymous function with one parameter i of type Integer.

Lambda Expression Life cycle :
Lambda expressions do have a life cycle as well, when compiler compiles a lambda expression then it first converts it to a function and then calls that function i.e,

i -> System.out.print(i) // lambda expression
is converted to:

public static void methodConvertedByLambda(Integer i) {
        System.out.println(i);
}

Java 8 Lambda Functional Interfaces

In java there are various interfaces that has only one method inside them. Those interfaces can be represented with Java 8 Lambda expressions. There is @FunctionalInterface annotation in java that we put above the interface. This will give compile time error if we assign more than one method.
Syntax :

@FunctionalInterface
interface MyInterface {
    public Integer add(Integer i1, Integer i2);
}

Return a lambda expression :
Example :

 @FunctionalInterface
interface MyInterface {
    public Integer add(Integer i1, Integer i2);
}

public static MyInterface sum() {
    return (j1, j2) - & gt;
    j1 + j2; // returning a lambda expression to further use as a an argument for method
}

public static Integer summation(MyInterface c) {
    int i = c.add(5, 4);
    return i;
}

public static void main(String[] args) {
    MyInterface c = sum();
    System.out.println(“Sum of two integers: ”+summation(c));
}

Output : 9
Description :
In above example, sum() returns a “ lambda expression “ which after then we have used as an argument for summation(MyInterface c) method.

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Leave a Reply

Your email address will not be published. Required fields are marked *