Statement lambda can be replaced with expression lambda Statement lambda can be replaced with expression lambda spring spring

Statement lambda can be replaced with expression lambda


Your statement lambda

param -> { return expression; }

can be changed to an expression lambda:

param -> expression

Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.


Sometimes I found useful to leave the braces where they are if the block of code is long enough (I think it improves readability)

In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below

//noinspection CodeBlock2Exprbutton.setOnClickListener((View v) -> {        //a long single method call...});


The Statement lambda can be replaced with expression lambda message appears when we use curly braces "{}" and semicolons ";" in expression lambdas for implementing functional interfaces. We can do away with those in such a situation. That being said, I personally feel it is a good coding practice to use block lambdas even for single line functional interface implementations, the way we do it for single line for and while loops.