Lambda functionality was introduced in Java 8 and has forever changed the landscape of programming in Java. The lambda functional design pattern uses lambda functions, also referred to as anonymous functions. These functions are passed as arguments to other functions.
Lambda functions have three parts:
- A single parameter: (argument)
- The arrow operator: ->
- The body: (body)
As with anything else in Java, there are a bunch of rules associated with even this seemingly simple syntax:
- If the parameter on the left has its type explicitly stated, it and the parameter must be encased in parenthesis, for example, (String a)
- The body can only call a single method
- The body must return a result
- For bodies with only one parameter, you do not need braces or a semicolon
- For bodies with more than one parameter, you do need braces and a semicolon
- You...