What is a lambda expression (Java)

I couldn't understand the lambda expression, so I've simplified it and understood the result.

Anonymous function

First, an excerpt from Wikipedia about anonymous functions.

Anonymous function (English: anonymous function or nameless function) [1] in a programming language is a function defined without being named.

There are various methods for expressing anonymous functions, but in recent years [2] the mainstream is the lambda expression notation.

(Wikipedia (https://ja.wikipedia.org/wiki/anonymous function))

Lambda expression

In other words, a lambda expression is a way to represent an anonymous function. ("->" Such a guy) → A lambda expression is a function that is defined without being named.

By the way, when writing in Java, it looks like this. (Excerpt from Wikipedia's anonymous function.)


import java.util.function.*;
BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y;
System.out.println(add.apply(2, 3));

↑ To explain with this example -"(X, y)" represents the argument of an anonymous function.

-"X + y" represents the processing of the function. (Although it is omitted in this example, it may be expressed as "{return x + y}". I think this is more method-like and easier to understand.)

Functional interface

What are you using anonymous functions for in the first place? That's why the functional interface comes out. Roughly speaking, it is an interface to which functions (see lambda expressions and methods) can be assigned.

For example, the interface BiFunction <T, U, R>

Type parameters: T --Type of first argument of function U --Type of second argument of function R-Function result type → An interface that allows you to assign a function that returns a return value with two arguments.

In other words, if you assign the anonymous function of the previous example to this interface and call the method (apply) defined in this interface, the processing of the anonymous function will be called. ↓ As for the result of this, since 2 and 3 are passed as arguments to apply, the result of 2 + 3 is output as standard.


BiFunction<Integer, Integer, Integer> add = (x, y) -> {return x + y;};
System.out.println(add.apply(2, 3));

Until now, I haven't thought about assigning functions to variables in Java, so I think it's difficult to understand. It seems to be a concept that comes up quite naturally when reading a JavaScript book.

Recommended Posts

What is a lambda expression (Java)
What is a lambda expression?
What is a Java collection?
What is java
What is Java <>?
What is Java
[Java] Lambda expression
Java lambda expression
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
java neutral lambda expression 1
Java lambda expression variations
What is Java Encapsulation?
Java 8 lambda expression Feature
java lambda expression memo
What is Java technology?
What is Java API-java
Java lambda expression [memo]
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
[Java] What is flatMap?
What is a Servlet?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Java Assertion? Summary.
What is a wrapper class?
What is a Ruby module?
What is a floating point?
[Java] Functional interface / lambda expression
What is a meaningful comment?
Java8 stream, lambda expression summary
What is a jar file?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
What is a fa⁉ enum?
[Java basics] What is Class?
What is java escape analysis?
What is JVM (Java Virtual Machine)?
Assign a Java8 lambda expression to a variable and reuse it
How slow is a Java Scanner?
Is Java on AWS Lambda slow?
Java basic learning content 9 (lambda expression)
What is thread safe (with Java)
What is a snippet in programming?
What is a column Boolean type?
What is a reference type variable?
[Java] What is Concurrent Modification Exception?
Java Calendar is not a singleton.
[Swift] What is "inheriting a class"?
What is a Ruby 2D array?
What is a terminal? -Absolute path & relative path-
What is a Spring Boot .original file?
Now let's recap the Java lambda expression
[For programming beginners] What is a method?
What is the best file reading (Java)
What is Java and Development Environment (MAC)
What is the main method in Java?
[Rails] What is a dot (.) Or a colon (:)?
What is a request scope? Image commentary
What is Cubby