Java lambda expression variations

I investigated the variation of writing style for lambda expressions introduced in Java8. I will cover it in a tabular format so that you can easily refer to it when you get lost.

Lambda-style variation

No. Notation Description
1 () -> System.out.println("foo") No arguments
2 (n) -> System.out.println(n) With arguments(Enclose in parentheses)
3 (int n) -> System.out.println(n) With arguments(Enclose in parentheses + type specification)
4 n -> System.out.println(n) With arguments(Do not enclose in parentheses)
* Parentheses can be omitted only when there is one argument
5 (m, n) -> m+n With arguments(Multiple)
6 (int m, int n) -> m+n With arguments(Multiple + type specified)
7 (n) -> { return n * 2;} Statement type lambda expression

Sample code

The above sample code.

// No.1 No arguments
Runnable lambda1 = () -> System.out.println("foo");
// No.2 with arguments(Enclose in parentheses)
IntConsumer lambda2 = (n) -> System.out.println(n);
// No.With 3 arguments(Enclose in parentheses + type specification)
IntConsumer lambda3 = (int n) -> System.out.println(n);
// No.4 with arguments(Do not enclose in parentheses)* Parentheses can be omitted only when there is one argument
IntConsumer lambda4 = n -> System.out.println(n);
// No.5 with arguments(Multiple)
IntBinaryOperator lambda5 = (m, n) -> m+n;
// No.6 with arguments(Multiple + type specified)
IntBinaryOperator lambda6 = (int m, int n) -> m+n;
// No.7 Statement type lambda expression
IntUnaryOperator lambda7 = (n) -> { return n * 2;};

Reference source

-[C #] Lambda style writing variation -[Java] Super introduction to Java 8 lambda (writing style, functional interface, original definition, lambda receiving process) -Standard functional interface -Basics of using Java8 lambda expressions -Java8 Lambda Summary

Recommended Posts

Java lambda expression variations
[Java] Lambda expression
Java lambda expression
java neutral lambda expression 1
Java 8 lambda expression Feature
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 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 regular expression 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
[Java] Introduction to lambda expressions
What is a lambda expression?
Memoization recursion with lambda expression
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Java lambda expressions learned with Comparator
[Introduction to Java] About lambda expressions
About Lambda, Stream, LocalDate of Java8
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
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.