Can a lambda expression have more than one parameter?
Answer:
Explanation:
A lambda expression in Java can have multiple parameters. When a lambda expression has more than one parameter, the parameters must be enclosed in parentheses and separated by commas. Here is an example of a lambda expression with two parameters:
(int a, int b) -> a + b
In this example, the lambda expression takes two parameters, a
and b
, and returns their sum. Lambda expressions with multiple parameters are commonly used in situations where the logic depends on more than one input, such as when implementing functions that combine or compare two values.
The ability to work with multiple parameters makes lambda expressions highly versatile, enabling developers to handle a wide range of scenarios in a concise and efficient manner. Whether you’re dealing with single or multiple inputs, lambda expressions provide a powerful way to encapsulate behavior in a function.