Java 8 lambda expression Feature

Java 8 introduced lambda expression to move toward functional programming. A lambda expression is an anonymous function that doesn’t have a name and doesn’t belong to any class.

Where to use the Lambda Expression A lambda expression can only be used where the type they are matched against is a single abstract method(SAM) interface or functional interface.

To use a lambda expression, you can either create your own functional interface or use the predefined functional interface provided by Java.

Example of a pre-defined interface: Runnable, callable, ActionListener, etc.

Pre Java 8: Use anonymous inner classes. Post-Java 8: Now use lambda expression instead of anonymous inner classes.

Points to remember Lambda expression is also known as a closure that allows us to treat functionality as a method arguments (passing functions around) or treat code as data. Lambda expression concept was first introduced in the LISP programming language. Lambda expression is used to provide an implementation of a functional interface or Single Method Interface. Lambda expression treated as a function so the compiler does not create .class file. Lambda expression doesn’t need to define a method again for implementation. Lambda expression benefit is less coding. Java Lambda expression Syntax To create a lambda expression, On the left side of the symbol lambda operator(->) specify input parameters (if there are any), and on the right side place the expression or block of statements.

(parameter_list) -> {function_body} For example, the lambda expression (x, y) -> x + y specifies that lambda expression takes two arguments x and y and returns the sum of these.

Note:

Optional type declaration: No need to declare the data type of a parameter. The compiler can inference the data type from the value of the parameter. The optional parenthesis around parameter: No needs to declare a single parameter in parenthesis. For multiple parameters, parentheses are required. Optional curly braces: For a single line of the statement, No need to use curly braces in the expression body. Optional return keyword: The compiler automatically returns the value if the body has a single expression statement to return the value. Curly braces are required to indicate that expression statement returns a value. Here is some Lamda expression example according to a number of arguments.

No Argument Syntax

()->{ //write some statemnet here } One Argument Syntax

(arg1)->{ //write some statemnet here } Two Argument Syntax

(arg1,arg2)->{ //write some statemnet here } Method vs Lambda Expression in Java A function (or method) in Java has four main parts:

  1. Name
  2. Parameter list
  3. Body
  4. return type.

A lambda expression has these main parts: Lambda expression only has a parameter list and body.

  1. No name – Lambda expression is an anonymous function that doesn’t have a name.
  2. Parameter list
  3. Body – This is the main part of the function where implementation is written.
  4. No return type – Don’t need to write a return statement explicitly. The compiler is able to infer the return type by checking the code.

Related on

Spring boot interview questions and answers

Recommended Posts

Java 8 lambda expression Feature
[Java] Lambda expression
Java lambda expression
java neutral lambda expression 1
Java lambda expression variations
java lambda expression memo
Java lambda expression [memo]
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
[Java] Functional interface / lambda expression
Java8 stream, lambda expression summary
Java basic learning content 9 (lambda expression)
What is a lambda expression (Java)
Hello Java Lambda
Now let's recap the Java lambda expression
Java 12 new feature summary
Java 13 new feature summary
java learning (conditional expression)
Quarkus saves Java Lambda! ??
Understand Java 8 lambda expressions
How to use Java API with lambda expression
About Java lambda expressions
Java8 to start now ~ forEach and lambda expression ~
Introduction to lambda expression
Explain Java 8 lambda expressions
Java table expression injection
Java 10 new feature summary
java regular expression summary
Java 14 new feature summary
Java Lambda Command Pattern
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Use Lambda Layers with Java
Memoization recursion with lambda expression
Assign a Java8 lambda expression to a variable and reuse it
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Java lambda expressions learned with Comparator
[Introduction to Java] About lambda expressions
Is Java on AWS Lambda slow?
Hello World on AWS Lambda + Java
Try an If expression in Java
Java
Getting started with Java lambda expressions
Java EE 8 (Jakarta EE 8) New feature summary
Java
The origin of Java lambda expressions
How to use Java lambda expressions
Comparison of thread implementation methods in Java and lambda expression description method
I want to ForEach an array with a Lambda expression in Java
Implement API Gateway Lambda Authorizer in Java Lambda
I tried to summarize Java lambda expressions
Nowadays Java lambda expressions and Stream API
[Java] for Each and sorted in Lambda
AWS Lambda with Java starting now Part 1
Working with huge JSON in Java Lambda
I've reviewed Java's lambda expression, stream API, six months after I started Java.