Lambda Expression use case and connection with Functional Interface

Amnesh Goel

I am currently learning about Lambda expressions in Java. As per my understanding it is a block of code that we can pass around so that it can be executed later. But I cannot think of examples when we want to execute the code later.

What are various scenarios where this is useful? Also what is the connection between Lambda Expression and Functional interface?

akhil_mittal

A Lambda expression is used when we want to execute a block of code later, once or multiple times. The actual point of using it is deferred execution and there are many reasons for executing code later, such as:

  1. We want to run the code in separate thread.
  2. We want to run the code multiple times.
  3. We want to run it at a specific point in an algorithm e.g. comparison operation while sorting.
  4. We want to run the code when some event happens e.g on click of a link etc.
  5. We want to run it only when necessary etc.

Connection between Lambda Expression and Functional Interface

A Functional interface is an interface which has only one abtract method (though it can have multiple default methods). We can supply a lambda expression whenever an object of an Functional Interafce is expected. Consider an example where we want to execute some action n times.

execute(n, () -> System.out.println("Java Rocks"));

To accept the lambda the second parameter of the method signature must be Functional Interface and in the above example we can use e.g Runnable.

public static void execute(int n, Runnable action) {
     for(int counter = 0; counter < n; counter ++) {
           action.run();
     }
}

The point to note here is that the Lambda Expression is executed when action.run() is called. You can read more about Lambda Expression here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Annotating the functional interface of a Lambda Expression

From Dev

Functional interface + lambda expression for sum of integers in an array

From Dev

How a lambda expression maps into a functional interface?

From Dev

Cast context of functional interface in lambda expression

From Dev

Kotlin: Use a lambda in place of a functional interface?

From Dev

Cannot convert functional interface with generic method into lambda expression

From Dev

How to create lambda expression for functional interface having generic method

From Dev

Disambiguate functional interface in lambda

From Dev

How to use a functional in this case?

From Dev

Lambda expression's signature does not match the signature of the functional interface method apply

From Dev

lambda expression on interface in Kotlin

From Dev

Use of Functional Interface in Java 8

From Dev

Functional Interface Implementations and use cases

From Dev

The target type of this expression must be a functional interface

From Dev

The target type of this expression must be a functional interface in java

From Dev

Kotlin function type instead functional interface lambda

From Dev

Lambda use case confusion

From Dev

Use lambda expression in another lambda expression

From Dev

The target type of this expression must be a functional interface -Java 8 Functoinal Interface

From Dev

Is there such a thing as a "Non-Functional Use Case"?

From Java

How to use functional interface returning void?

From Dev

How to use functional interface returning void?

From Dev

Use a 'in' statement inside a 'case' expression

From Dev

Can an interface somehow prevent lambda expression implementations?

From Java

lambda expression for Consumer Interface with return type

From Dev

Implementing an interface with two abstract methods by a lambda expression

From Dev

lambda expression for Consumer Interface with return type

From Dev

Java lambda expression - how interface name was omitted?

From Dev

does Lambda expression work on one interface?

Related Related

  1. 1

    Annotating the functional interface of a Lambda Expression

  2. 2

    Functional interface + lambda expression for sum of integers in an array

  3. 3

    How a lambda expression maps into a functional interface?

  4. 4

    Cast context of functional interface in lambda expression

  5. 5

    Kotlin: Use a lambda in place of a functional interface?

  6. 6

    Cannot convert functional interface with generic method into lambda expression

  7. 7

    How to create lambda expression for functional interface having generic method

  8. 8

    Disambiguate functional interface in lambda

  9. 9

    How to use a functional in this case?

  10. 10

    Lambda expression's signature does not match the signature of the functional interface method apply

  11. 11

    lambda expression on interface in Kotlin

  12. 12

    Use of Functional Interface in Java 8

  13. 13

    Functional Interface Implementations and use cases

  14. 14

    The target type of this expression must be a functional interface

  15. 15

    The target type of this expression must be a functional interface in java

  16. 16

    Kotlin function type instead functional interface lambda

  17. 17

    Lambda use case confusion

  18. 18

    Use lambda expression in another lambda expression

  19. 19

    The target type of this expression must be a functional interface -Java 8 Functoinal Interface

  20. 20

    Is there such a thing as a "Non-Functional Use Case"?

  21. 21

    How to use functional interface returning void?

  22. 22

    How to use functional interface returning void?

  23. 23

    Use a 'in' statement inside a 'case' expression

  24. 24

    Can an interface somehow prevent lambda expression implementations?

  25. 25

    lambda expression for Consumer Interface with return type

  26. 26

    Implementing an interface with two abstract methods by a lambda expression

  27. 27

    lambda expression for Consumer Interface with return type

  28. 28

    Java lambda expression - how interface name was omitted?

  29. 29

    does Lambda expression work on one interface?

HotTag

Archive