On Lambda Expression in Java

  • 2021-12-12 08:26:38
  • OfStack

In this article, we will discuss about Java In Lambda Expression, Lambda The expression is Java Get involved in the process of functional programming. It accepts parameters and applies them to expressions or blocks of code. The following is a basic example of syntax:


(parameter1, parameter2) => expression

Or


(parameter1, parameter2) => {code block}

Lambda The expression is very limited if it is not void You must return 1 value immediately. They can't use such as if Or for Keywords like this to keep it simple. If you need more lines of code, you can use code blocks instead.

Now in the implementation lambda Expression, you cannot use only expressions. Lambda Is the implementation of a functional interface. A functional interface is an interface with only one abstract method. lambda The advantage of is that they allow you to implement methods without implementing interface classes and instantiated objects.

Here's an example:


interface FuncInterface
{
    //  Abstract function 
    void abstractFun(int x);

    //  Non-abstract (or default) functions 
    default void normalFun()
    {
       System.out.println("Hello");
    }
}

class Test
{
    public static void main(String args[])
    {
        //  Object that implements the above functional interfaces  lambda  Expression.  
        //  The default implementation of this interface  abstractFun()
        FuncInterface fobj = (int x)->System.out.println(2*x);

        //  This will call the above  lambda  Expression and print  10 . 
        fobj.abstractFun(5);
    }
}

Lambda Expressions are usually used as arguments to functions. To improve readability, you can also add lambda Expressions are stored in variables as long as the type is an interface with only 1 method, the same number of parameters, and the same return type.


import java.util.ArrayList;
import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1);
    Consumer<Integer> method = (n) -> { System.out.println(n); };
    numbers.forEach( method );
  }
}

lambda One common use of is to create threads. This is one that uses lambda Code block implementation Runnable Object for thread execution.


//  Runnable  Lambda
Runnable task2 = () -> { System.out.println("Task #2 is running"); };

//  Startup thread 
new Thread(task2).start();

As beginners, most of us are taught to program using the OOP concept, so it can be awkward to use different paradigms, such as functional programming. I am still learning these concepts myself. Anyway, I hope this article can make everyone learn something. If you have more questions or want to delve into this topic, please comment or check the materials below. The extracted code examples are also from the following resources

References:

www. geeksforgeeks. org/lambda-expr …
www. developer. com/microsoft/s …
www. codejava. net/java-core/t …


Related articles: