Chapter 15: Functional Programming Flashcards
Which functional interface from java.util.function takes 0 parameters and returns a generic object (T)?
Supplier
Which functional interface from java.util.function takes 1 parameter and returns void?
Consumer
Which functional interface from java.util.function takes 2 parameters and returns void?
BiConsumer
Which functional interface from java.util.function takes 1 parameter and returns a boolean?
Predicate
Which functional interface from java.util.function takes 2 parameters and returns a boolean?
BiPredicate
Which functional interface from java.util.function takes 1 parameter and returns a generic object (R)?
Function
Which functional interface from java.util.function takes 2 parameters (T, U) and returns a generic object (R)?
BiFunction
Which functional interface from java.util.function takes 1 parameter (T) and returns a generic object (T)?
UnaryOperator
Which functional interface from java.util.function takes 2 parameters (T, T) and returns a generic object (T)?
BinaryOperator
When should the Supplier interface be used?
When you want to generate or supply values without taking any input.
What method does the Supplier functional interface have?
get()
When should the Consumer interface be used?
When you want to do something with a parameter but not return anything.
What method does the Consumer and BiConsumer functional interfaces have?
accept(T)
accept(T, U)
What method does the Predicate and BiPredicate functional interfaces have?
test(T)
test(T, U)
What method does the Function and BiFunction functional interfaces have?
apply(T)
apply(T, U)
When should the Function interface be used?
When you want to turn one parameter into a value of potentially different type and return it.
Which functional interface should be used when you want to combine two strings and return a new one?
BiFunction or BinaryOperator (better)
UnaryOperator and BinaryOperator are similar to Function and BiFunction. How do they differ?
UnaryOperator and BinaryOperator require the input and output to be of the same type.
What interface does UnaryOperator extend?
Function
What interface does BinaryOperator extend?
BiFunction
Is the following code valid?
UnaryOperator u1 = String::length;
No. The statement returns an Integer but UnaryOperator requires the return type be the same as the input type (String).
To make this code valid, Function needs to be used.
What functional interface would you use in the following situation:
Returns a String without taking any parameters.
Supplier
What functional interface would you use in the following situation:
Returns a Boolean (not a primitive) and takes a String
Function
Predicate returns a primitive boolean.
What functional interface would you use in the following situation:
Returns an Integer and takes two Integers.
BinaryOperator or BiFunction (both are equivalent, but BinaryOperator is more specific).
What functional interface would you use in the following situation:
Returns a boolean primitive and takes a String
Predicate
Predicate returns a primitive boolean. Function could be used to return a Boolean (Wrapper)
What functional interface(s) belong on the blank lines?
____ ex = x -> ““.equals(x.get(0));
It pases one List and returns a boolean. Therefore this could be a Predicate or a Function.
What functional interface(s) belong on the blank lines?
____ ex = (Long l) -> System.out.println(l);
It passes a long and doesn’t return anything. Therefore this should be a Consumer.
What functional interface(s) belong on the blank lines?
____ ex = (s1, s2) -> false;
BiPredicate
Takes two parameters and returns a boolean. Since the generics don’t specify a Boolean, it can’t be a Function.
What is wrong with the following statement?
Function< List> ex = x -> x.get(0);
A Function always needs to specify two generics, one input and one output type.
What is wrong with the following statement?
UnaryOperator ex = (long l) -> 3.14;
To use a UnaryOperator, the same type must be returned (in this case a Long). The statement returns a double instead.
What is wrong with the following statement?
Predicate ex = String::isEmpty;
Predicate expects a generic but since it wasn’t supplied, the parameter that was passed in is treated as an Object, instead of a String.
What convenience methods does Predicate contain?
- and()
- negate()
- or()
How can the following code be rewritten using conveniance methods?
Predicate brownEggs = s -> s.contains(“egg”) && s.contains(“brown”);
Predicate egg = x -> x.contains(“egg”);
Predicate brown = x -> x.contains(“brown”);
Predicate brownEgg = egg.and(brown);
How can the following code be rewritten using conveniance methods?
Predicate brownEggs = s -> s.contains(“egg”) && !s.contains(“brown”);
Predicate egg = x -> x.contains(“egg”);
Predicate brown = x -> x.contains(“brown”);
Predicate brownEgg = egg.and(brown.negate());
What convenience methods does Consumer contain?
- andThen()
What convenience methods does Function contain?
- andThen()
- compose()
What conveniance method can be used to combine the following consumers?
Consumer c1 = x -> System.out.println(x);
Consumer c2 = x -> System.out.println(x);
andThen()
Consumer combined = c1.andThen(c2);
What conveniance method can be used to pass the output of f1 to the input of f2?
Function f1 = x -> x + 1;
Function f2 = x -> x * 2;
compose()
Function combined = f2.compose(f1);
What does the following code do?
Optional opt = Optional.empty();
opt.get();
Throws a NoSuchElementException
How do we prevent the code from throwing an exception (without using a try-catch)?
Optional opt = Optional.empty();
opt.get();
replace opti.get() with:
if (opt.isPresent())
opt.get();
What does the Optional static method ofNullable() do?
Creates an empty optional if the provided value is null or creates an optional with the value if it is not null.
What does the Optional instance method ifPresent(Consumer c) do if the Optional is empty?
Does nothing.
What does the Optional instance method ifPresent(Consumer c) do if the Optional contains a value?
Calls Consumer with value.
What does the Optional instance method orElse(T other) do if the Optional contains a value?
Returns value
What does the Optional instance method orElse(T other) do if the Optional is empty?
Returns other parameter
How can we use Optional to get the value, or else throw a specific exception if it is empty?
Using the method orElseThrow().
We can leave the parameters empty to throw a NoSuchElementException, or provide a Supplier to throw another exception.
Why does the following code not compile?
Optional{Double} opt = Optional.empty(); System.out.println(opt.orElseGet(() -> new IllegalStateException()));
The opt variable is an Optional with generic Double which means the Supplier must return a Double. This Supplier returns an exception instead.
What is a stream in Java?
A stream in Java is a sequence of data.
What is a stream pipeline in Java?
A stream pipeline consists of the operations that run on a stream to produce a result.
What are the parts of the stream pipeline?
- source: where the stream comes from
- Intermediate operations: Transforms the stream into another one.
- Terminal operation: actually produces a result. The stream is no longer valid after a terminal operation completes.
The Stream interface is located in what package?
java.util.stream
What are ways to create finite Streams?
- Stream.empty()
- Stream.of(varargs)
- someCollection.stream()
- someCollection.parallelStream()
- Streams.iterate(seed, predicate, unaryOperator)