Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Lambda expressions - Java 8

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    Lambdas?

    A lambda is nothing but a anonymous function which accepts zero or more arguments and produces output. Lambda is basically composed with three components- argument section, an arrow and a body.

    (argument ...) -> { body } // a valid pseudo code 
    

    Example 1 :

     x -> System.out.println(x);
    

    Example 2 :

    (x,y) -> return x + y;
    

    Example 3 :

    (int x, int y) -> { int z = x + y;
    return z;
    }
    

    Let me explain the examples.
    First example, shows that brackets are optional if there is only a single argument.
    In second example, Type of the arguments are specified. Compiler checks the type if not declared, it enable runtime typecasting for primitives. Developers can ignore the curly brackets around a single line body.
    Third example shows the argument types and the body of the function.

    Lambda expressions can be used to define a inline implementation of a functional interface i.e. an interface with a single method only. For example - Java developer are well familiar with the example below -

    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello from thread");
        }
    }).start();
    
    But it could be done more easily with the Lambda expressions as following -
    new Thread(
        () -> System.out.println("Hello from thread")
    ).start();
    

    In the above example, we instantiate the Runable and define the body of run method.

    It is very useful in inner class declaration. For example :

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("The button was clicked using old fashion code!");
        }
    });
    

    It can be done as below.

    button.addActionListener( (e) -> {
            System.out.println("The button was clicked. From lambda expressions !");
    });
    

    Here, we don't need to create a inner class to handle the button event.

    Lambda expressions are very useful while working with Collections. For example -

    List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    list.forEach(n -> System.out.println(n));
    

    @FunctionalInterface

    Similar to the marker interface, there is a concept of functional interface. The interface with only a single methods are known as functional interface. @FunctionalInterface is a new interface added in Java 8 to indicate that we can simply use it in our API and take advantage of Lambda expressions. For example -

    @FunctionalInterface
    public interface MyFunctionalInterface {
    
        public void doSomeWork();
    
    }
    
    
    public class MyFunctionalInterfaceTest {
    
        public static void execute(MyFunctionalInterface worker) {
            worker.doSomeWork();
        }
    
        public static void main(String [] args) {
            execute( () -> System.out.println("Worker invoked using Lambda expression") );
        }
    
    }
    

    Have fun with Java 8 and Lambda expressions.

    Happy coding.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: