OCP - 2
Functional Programming, Dates, Strings, Localization, Exceptions and Assertions
Functional Programming, Dates, Strings, Localization, Exceptions and Assertions
Kartei Details
Karten | 115 |
---|---|
Sprache | English |
Kategorie | Informatik |
Stufe | Andere |
Erstellt / Aktualisiert | 22.02.2019 / 08.01.2020 |
Weblink |
https://card2brain.ch/box/20190222_ocp_2
|
Einbinden |
<iframe src="https://card2brain.ch/box/20190222_ocp_2/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
What is the return type of Predicate?
Primitive boolean (prmitive)
What common Built-In Functional Interfaces do exist?
- Supplier
- Consumer
- BiConsumer
- Predicate
- BiPredicate
- Function
- BiFunction
- UnaryOperator
- BinaryOperator
In which package can the built-in functional interfaces be found?
java.util.functional
Does the Predicate class define only exactly one method?
Only one abstract method, but some additional utility default implementations, such as and.
This makes it's use convenient: eg, given two predicates egg and brown: egg.and(brown)
What is the goal of the Function Interface?
Turning one parameter into a value of a potentially different type and returning it
What is the goal of the Unaryperator Interface?
A special case of Function, for which the Input and Output Parameter are the same. It is actually a subclass of the Function interface
What happens here?
Predicate ex = String::isEmpty;
It is missing the generic for Predicate. This makes the parameter that was passed an Object rather than a String. The lambda expects a String because it calls a method that eists on String rather than Object. Therfore, it does not compile
What static factory methods exist on Optional?
- Optional.empty()
- Optional.of(123)
- Optional.ofNullable(val)
What instance methods exist on Optional?
- opt.isPresent()
- opt.get()
- ifPresent(Consumer c)
- orElse(T other)
- orElseGet(Supplier s)
- orElseThrow(Supplier s)
What happens when get() is called on an Optional that is empty?
An exception is thrown
Of what does a stream consist?
- Source
- Intermediate operations
- Terminal operation
In wich package are stream related functions?
java.util.sream
What are common factory methods to create a stream?
- Stream.empty()
- Stream.of(1, 2, 3)
- aCollection.stream()
- aCollection.para;parallelStream()
- Stream.generate(Math::random)
- Stream.iterate(1, n -> n + 2)
What are Reductions?
A special type of terminal operation where all of the contents of the stream are cobined into a single primitive or Object
What are the terminal stream operations?
- allMatch() / anyMatch() / noneMatch()
- collect()
- count()
- findAny()/findFirst()
- forEach()
- min()/max()
- reduce()
Why do some terminal operations return an Optional, eg. min
Because a stream can be empty!
Can stream be used in a for each loop?
No
What are the signatures of reduce?
- T reduce(T identity, BinaryOperator<T> accumulator)
- Optional<T> reduce(BinaryOperator<T> accumulator)
- <U> U reduce(U identity, BinaryOperator<U, ? super T, U > accumulator, BinaryOperator<U> combiner)
What is the goal of Stream.collect()?
To create a mutable reduction for performance, eg. for StringBuilder or Lists:
Stream<String> stream = Stream.of("a", "b");
TreeSet<String> set = stream.collect(TreeSet::new, TreeSet::add, TreeSet::addAll)
AddAll can combine multiple subsets of Treeset - used when processed in parallel!
What common intermediate Operations exist?
- filter
- distinct
- limit/skip
- map
- flatMap
- sorted
- peek
What does stream.flatMap do?
Takes each element in the stream Makes any element it containes in a single stream
What does stream.peek do?
It allows us to perfomr a stream operation without actually changing the stream
How can you work with primitives in streams?
You have to use the IntStream, LongStream and DoubleStream
Are the primitive streams identical to object streams?
No, they provide additional functionallity such as average
what does IntStream.range(1, 6) produce?
A stream of the numbers 1 2 3 4 and 5
How can you convert a primitive STream into a object stream?
With the mapToObj method
Why does OptionalDouble exist?
For double (primitive type)
What does ints.summaryStatistics return?
An object containing common stats such as min, max and average
What the name of the single abstract method of DoubleSupplier, IntSupplier and LongSupplier?
getAsDouble
getAsInt
getAsLong
What the name of the single abstract method of DoubleConsuer, IntConsumer and LongConsumer?
accept
What the name of the single abstract method of DoublePredicate IntPredicate and LongPredicate?
test
What the name of the single abstract method of DoubleFunction<R>, IntFunction<R> and LongFunction<R>?
R apply(double|int|long arg)
What the name of the single abstract method of DoubleUnaryOperator, IntUnaryOperator and LongUnaryOperator?
applyAsDouble
applyAsInt
applyAsLong
Whats the singature of the single abstract method of IntFunction?
<R> apply(int i)
What common Functional Interfaces do not exist for primitives?
BiConsumer
BiPredicate
BiFunction
-> Not used often with primitives
Whats the singature of the single abstract method of ToIntFunction?
int applyAsInt(T arg)
Which functional interface would you use to fill in the blank to make the follwing code compile?
double d = 1.0;
____ f1 = x -> 1;
f1.applyAsInt(d);
double d = 1.0;
DoubleToIntFunction f1 = x -> 1;
f1.applyAsInt(d);
Accepts a double and returns an int (implied by the name applyAsInt)
How does Optionals Chaining works?
Analogous to streams, Optional provides map, filter and other operations that make the code easier to read.
The Intermediate operations such as map accept a method that is not aware of an optional, eg: Optional<Integer> result = optional.map(String::length). If String.lenght would return an optional instead, flatMap must be used.
Can streams handle functional interfaces that declare cheked exceptions?
No, all functional interfaces do not define checked exceptions and are therefore incompatible with methods that do so.
To work with such methods, RuntimeException Wrappers must be used
What is the Function.identity() method for?
Is an expressive shorthand for the simple lambda function o -> o