Ch15 Functional Programming Flashcards
Which functional interface from java.util.function takes 1 parameter (T) and returns a generic object (T)?
UnaryOperator
What method does the Supplier functional interface have?
get()
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)
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 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?
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 NonBrownEggs = s -> s.contains(“egg”) && !s.contains(“brown”);
- Predicate egg = x -> x.contains(“egg”);
- Predicate brown = x -> x.contains(“brown”);
- Predicate NonBrownEggs = 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.
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()));
No it doesn’t.
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 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()
Stream.iterate(seed, predicate, unaryOperator)
Given the following list (or any other Collection), how do we create a stream out of this?
var list = List.of(“a”, “b”, “c”);
Stream fromList = list.stream();
Given the following list (or any other Collection), how do we create a parallel stream out of this?
var list = List.of(“a”, “b”, “c”);
Stream fromList = list.parallelStream();
What are ways to create infinite Streams?
- Stream.generate(supplier)
- Stream.iterate(seed, unaryOperator)
- Stream.iterate(seed, predicate, unaryOperator)
Make a statement for creating an infinite stream that returns random numbers.
Stream randoms = Stream.generate(Math::random);
Make a statement for creating an infinite stream that returns every odd number.
Stream oddNums = Stream.iterate(1, n -> n + 2);
Make a statement for creating an infinite stream that returns every odd number, less than 100.
Stream oddNums = Stream.iterate(1, n -> n < 100, n -> n + 2);