6.1-Writting Simple Lambdas Flashcards
📝️What is Functional programming?
It’s a way to write code in a declarative way.
- > So you specify what you want to do, rather than dealing with the state of objects.
- > You focus more on expressions than loops
📝️How do we write in a Functional Programming style in Java?
By using Lambda Expressions …
aka Lambdas, Clousures
📝️What is a Lambda Expression?
A lambda expression is a concise way to implement the Single Abstract Method of a Functional Interface
📝️Why Lambdas? (4)
- Enable Functional Programming (Declarative instead of Imperative)
- Write a more comprehensible code
- > This is more Readable and Concise) - Easier to use API’s & Libraries
- Enables Parallel Programming
📝️How to write Lambdas (Syntax)?
param-list -> statement(s);
param-list ::
- > 4 single-parameter (WITHOUT type) parentheses are optional.
- > parameter-type is optional when using parameter *** Type is infered by context (Functional Interfaces SAM)
-> Arror-operator
statement(s) ::
- > For single (return) statement curly braces are optional
- > If return keyword present or multiple statements curly braces are mandatory.
📝️ What are the predifined Functional Interfaces? (4)
Predicate -> boolean test(T t)
Function -> R apply(T t)
Consumer -> void accept(T t)
Supplier -> T get()
📝️ How to implement a Functional Interface?
- ** An interface can NOT be instantiated
1. A concrete class that overrides the SAM method
2. An Anonymous Class that overrides the SAM method
3. A Lambda Expression
4. Method Reference (A method with the same signature that is already implemented)
When a Lambda Expression can be used?
When implementing the SAM of a Functional Interface
How does a Lambda Expression infere the parameter types?
Because of the Context, a Lambda Expression implements a SAM from a Functional Interface