|
|
Lambda Expressions
Author: Venkata Sudhakar
Lambda expressions are a key feature introduced in Java 8 that allow you to write concise functional-style code. They enable you to treat functionality as a method argument and write inline implementations of functional interfaces. Lambda Expression Syntax: (parameters) -> expression or (parameters) -> { statements; } The below example shows various ways to use Lambda Expressions in Java 8.
It gives the following output,
All names:
Alice
Bob
Charlie
David
Eve
Names longer than 4 chars:
Alice
Charlie
David
Uppercase names:
ALICE
BOB
CHARLIE
DAVID
EVE
Sorted by length: [Bob, Eve, Alice, David, Charlie]
Hello, World!
Functional Interfaces used with Lambda: Predicate<T> - Takes T, returns boolean. Used for filtering. Function<T,R> - Takes T, returns R. Used for transformation. Consumer<T> - Takes T, returns void. Used for side effects. Supplier<T> - Takes nothing, returns T. Used for lazy creation.
|
|