Lambda and Anonymous function Java
Lambda Function and Anonymous function
Defination
A more concise way to represent anonymous classes that implement certain interfaces
Grammar
1 | (Parameters) ->{Code Block} |
eg:
without para
1 | () -> System.out.println("Hello, World!") |
With one para
1 | x -> System.out.println(x) |
with more para
1 | (x, y) -> x + y |
combine with the function
1 | Runnable run = () -> System.out.println("Running..."); |
Anonymous function
Defination
An unnamed function
Often used for brief, one-time operations
Compare beteween Lambda and Anonymous Function
lambda
1 | List<String> names = Arrays.asList("Anna", "Bob", "Charlie"); |
anonymous function
1 | List<String> names = Arrays.asList("Anna", "Bob", "Charlie"); |