Functional Programming Flashcards
What are the variable access rules for lambdas? what else shares these same rules?
lambdas can access static variables, instance variables, and effectively final local variables, effectively final method parametersinner classes share same rules
Supplier functional interface: What are the - params - return type - single abstract method
Supplier- 0 params- return type T- get
Consumer functional interface: What are the - params - return type - single abstract method
Consumer- 1 (T)- void- accept
BiConsumer functional interface: What are the - params - return type - single abstract method
BiConsumer- 2 (T, U)- void- accept
Function functional interface: What are the - params - return type - single abstract method
Function- 1 (T)- return type R- apply
BiFunction functional interface: What are the - params - return type - single abstract method
BiFunction- 2 (T, U)- return type R- apply
Predicate functional interface: What are the - params - return type - single abstract method
Predicate- 1 (T)- return type boolean- test
BiPredicate functional interface: What are the - params - return type - single abstract method
BiPredicate- 1 (T, U)- return type boolean- test
UnaryOperator functional interface: What are the - params - return type - single abstract method
UnaryOperator- 1 (T)- return type T- apply
BinaryOperator functional interface: What are the - params - return type - single abstract method
BinaryOperator- 2, (T, T)- return type T- apply
What is a supplier functional interface often used for?
supply no paramsreturn something- creating new objects Supplier> s1 = ArrayList::new;ArrayList a1 = s1.get();
what is a consumer functional interface often used for?
- supply 1 or 2 params- return void
what is predicate functional interface often used for?
- supply 1 or 2 params- return boolean
What are 2 default methods on predicates used to chain predicates?
Predicate.and(Predicate)- can combine together predicate conditionsPredicate.negate()- can negate a predicate condition
What is a Function/BiFunction functional interface often used for?
- supply 1 or 2 params- return same or different type
What is a UnaryOperator/BinaryOperator functional interface often used for? what other functional interface is it related to?
- supply 1 or 2 params of same type- return same type- specialized version of Function/BiFunction
What does Optional.empty() return?
an Optional object with no value
What happens if you have an empty Optional and call optional.get()
NoSuchElementException is thrown
What Optional factory method would you use to wrap a value if it exists, or wrap empty if a value does not exist?
Optional.ofNullable(value);
How would you check if an Optional has a value?
optional.isPresent()
How would you get the value from an Optional?
optional.get()
How can you call Consumer c with value if an Optional value is present? What happens if value is not present?
anOptional.ifPresent(Consumer c)nothing
How can you define a default value if none exists in the Optional? What happens if value is present?
“anOptional.orElse(““value”“)value returned”
How can you call a Supplier s if an Optional value is not present? What happens if value is present?
anOptional.orElseGet(Supplier s)value returned
How can you thrown an exception using a Supplier if Optional value is not present? What happens if value is present?
anOptional.orElseThrow(Supplier s)value returned
What is stream in Java?
a sequence of data
What is a stream pipeline?
operations that run on a stream to produce a result
what are the 3 parts of a stream pipeline?
source - where stream comes fromintermediary operations - transforms stream into anotherterminal operation - actually produces a result
how many times can stream result from a terminal operation be used?
once
how many times can stream result from a intermediary operation be used?
as many times as you want
when are intermediary vs terminal operations called?
intermediary - lazy evaluation - not called until terminal operation runterminal - run when method called
does an intermediary or a terminal operation return a stream type?
intermediary - yesterminal - no