Functional Programming Flashcards
What types of variables can lambda expressions use?
static variables
instance variables
effectively final variables in methods (effectively final meaning they are only ever assigned one value and never altered)
Functional Interface
Supplier
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 0
2) T
3) get
Functional Interface
Consumer
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 1 (T)
2) void
3) accept
Functional Interface
BiConsumer
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 2 (T, U)
2) void
3) accept
Functional Interface
Predicate
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 1 (T)
2) boolean
3) test
Functional Interface
BiPredicate
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 2 (T, U)
2) boolean
3) test
Functional Interface
Function
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 1 (T)
2) R
3) apply
Functional Interface
BiFunction
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 2 (T, U)
2) R
3) apply
Functional Interface
UnaryOperator
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 1 (T)
2) T`
3) apply
Functional Interface
BinaryOperator
1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?
1) 2 (T, T)
2) T`
3) apply
When do you use the Supplier functional interface?
When you want to generate or supply values without taking any input.
Think of it as creating something to supply
When do you use the Consumer or BiConsumer functional interface?
When you want to do something with a parameter (or two if BiConsumer) but not return anything.
Think of it as consuming the parameters
When do you use the Predicate or BiPredicate functional interface?
When filtering or matching.
It checks for a certain condition and returns true or false.
When do you use the Function or BiFunction functional interface?
When turning parameters into a value of a potentially different type and returning it.
Use the parameters to calculate/determine a result
When do you use the UnaryOperator or BinaryOperator functional interface?
When you want to transform a value (or two) into another value of the same type.
UnaryOperator extends Function
BinaryOperator extends BiFunction
Optional
A container object which may or may not contain a non-null value
How to represent an empty Optional object?
Optional.empty()
How to represent an Optional object with a value of 30?
Optional.of(30)
Optional.ofNullable(val)
Returns an Optional.empty() if val is null, or Optional.of(val) if val is non-null
Optional instance method
get()
When the Optional is empty, throws an exception
When the Optional contains a value, returns the value
Optional instance method
ifPresent(Consumer c)
When the Optional is empty, does nothing
When the Optional contains a value, calls Consumer c with the value
Optional instance method
isPresent()
When the Optional is empty, returns false
When the Optional contains a value, returns true
Optional instance method
orElse(T other)
When the Optional is empty, returns other
When the Optional contains a value, returns the value
Optional instance method
orElseGet(Supplier s)
When the Optional is empty, returns result of calling Supplier s
When the Optional contains a value, returns the value
Optional instance method
orElseThrow(Supplier s)
When the Optional is empty, throws the exception created by calling Supplier s
When the Optional contains a value, returns the value