[Java] Summary of how to abbreviate lambda expressions

Here's a summary of how to abbreviate lambda expressions. It will be asked in exams such as Java Silver, so I hope it will help the examinees. Finally, I will summarize how to remember.

If you don't understand the lambda expression in the first place, please click here. Understanding Java 8 Lambda Expressions

First, the basic form of the lambda type.

Uninflected word


Predicate<String> javaChecker = (String s) -> { return s.equals("Java"); };

Let's see what can be omitted from here.

Argument type

First, the argument types are optional.

Predicate<String> javaChecker = (s) -> { return s.equals("Java"); };

However, if there are multiple arguments, it is not possible to omit only one.

//Compile error
BiFunction<Integer, Integer, Integer> adder = (a, Integer b) -> { return a + b; };

Argument ()

The argument () can also be omitted.

Predicate<String> javaChecker = s -> { return s.equals("Java"); };

However, there are two conditions. First, if you specify a type, you cannot omit it.

//Compile error
Predicate<String> javaChecker = String s -> { return s.equals("Java"); };

Second, it cannot be omitted even if there are multiple arguments.

//Compile error
BiFunction<Integer, Integer, Integer> adder = a, b -> { return a + b; };

I think it's good to remember that () is optional only when the argument is one word.

Method {}

The method {} can also be omitted.

Consumer<String> buyer = goods -> System.out.println(goods + "I bought");

There are also conditions here. First, it cannot be omitted if there are two or more sentences.

//Compile error
Consumer<String> buyer = goods -> String message = "I," + goods;
                                  System.out.println(message + "I bought");

If you write return next, you cannot omit it.

//Compile error
Predicate<String> javaChecker = s -> return s.equals("Java");

However, if you omit return, you can execute it normally.

Predicate<String> javaChecker = s -> s.equals("Java");

Remember that {} can be omitted only when the process is one statement, and if there is a return value, the return can be omitted as a set.

return As mentioned above, return can be omitted, but it cannot be omitted if the method {} is left.

//Compile error
Predicate<String> javaChecker = s ->{ s.equals("Java"); };

Even if you want to omit the return, it will be a set with the omission of {}.

Summary

The summary is as follows.

Omitted part conditions
Argument type 全Argument typeを省略すること
Of the argument() Only for one word
Of the method{} Only for one statement
If there is a return value, set with omission of return
return Of the method{}Omission and set

Recommended Posts

[Java] Summary of how to abbreviate lambda expressions
How to use Java lambda expressions
[Java Silver] Summary of points related to lambda expressions
[java] Summary of how to handle char
[Java] [Maven3] Summary of how to use Maven3
[java] Summary of how to handle character strings
[Java] Summary of regular expressions
[Java] Introduction to lambda expressions
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
[Introduction to Java] About lambda expressions
The origin of Java lambda expressions
How to make a Java calendar Summary
I tried to summarize Java lambda expressions
Summary of how to write annotation arguments
Summary of how to select elements in Selenium
Summary of how to create JSF self-made tags
How to use Java framework with AWS Lambda! ??
Understand Java 8 lambda expressions
How to use Java API with lambda expression
About Java lambda expressions
Explain Java 8 lambda expressions
[Java] How to get the authority of the folder
Summary of Java support 2018
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
[Java] Types of comments and how to write them
How to deploy Java to AWS Lambda with Serverless Framework
Use Java lambda expressions outside of the Stream API
[Java] How to get the maximum value of HashMap
Summary of knowledge required to pass Java SE8 Silver
As of April 2018 How to get Java 8 on Mac
How to execute WebCamCapture sample of NyARToolkit for Java
Notes on how to use regular expressions in Java
[Java] How to use Map
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
[Java11] Stream Summary -Advantages of Stream-
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] Summary of operators (operator)
[Java] How to use Optional ②
Java8 stream, lambda expression summary
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
[Java] Summary of control syntax