11.4. How to Create Lambda Expressions¶
The easiest way to create a lambda expression is by pretending to assign your intended method override to a variable of the interface type. Here’s an example:
Assign your intended method override to a variable of the interface type:
// This won't compile, but it's a good way to get started Consumer<String> shout = @Override public void accept(String t) { return System.out.println(t.toUpperCase()); };
Remove the annotation (
@Override
), the visibility modifier (public
), the return type (void
) and the method name (accept
):// this still won't compile, but it's only two characters away from working Consumer<String> shout = (String t) { System.out.println(t.toUpperCase()); };
Add an arrow (
->
) between the parameter list and the opening curly brace:// this WILL compile; we provided a valid lambda expression! Consumer<String> shout = (String t) -> { System.out.println(t.toUpperCase()); };
The three-step process above leads to a valid lambda expression (try it); however, there’s still room for improvement.
If the method body only contains a single statement, then we can omit the curly braces all together and write the lambda expression on a single line:
Consumer<String> shout = (String t) -> System.out.println(t.toUpperCase());
If the only statement is a return statement, then the keyword return is omitted when writing a lambda expression without curly braces.
Specifying the parameter types is optional.
Consumer<String> shout = (t) -> System.out.println(t.toUpperCase());
If we don’t include the parameter types in our lambda expression, then Java will try to determine what they are based on context. For example, if we’re assigning the created object to a
Consumer<String>
variable, then Java knows that the parameter list foraccept
is(String t)
and will automatically convert(t)
to(String t)
.If there is exactly one method parameter, then the parentheses for the parameter list are optional.
Consumer<String> shout = t -> System.out.println(t.toUpperCase());
If the method doesn’t have any parameters, then parentheses are still required, and
() ->
is used.
Test Yourself
Use a lambda expression to create a consumer that prints the length of a string.
Use a lambda expression to create a consumer that prints the number of exclamation marks in a string.
Use a lambda expressionto create a consumer that appends a string to a list.
Test Yourself Solutions (open after completing the questions above)
One possible solution:
Consumer<String> printLength = s -> System.out.println(s.length());
We won’t give exact solutions to the other two questions
above. However, you can try it and test the result! To ahead and
write your lambda expressions in the main
method of the
Driver
class you compiled in the previous section. Then,
compile and run the code to make sure it works as expected. If you
get stuck, post your question and the lambda expression you used to
Piazza for assistance.