java 2 3
fdsa
fdsa
Fichier Détails
Cartes-fiches | 496 |
---|---|
Langue | Deutsch |
Catégorie | Informatique |
Niveau | Autres |
Crée / Actualisé | 06.12.2020 / 24.01.2021 |
Lien de web |
https://card2brain.ch/box/20201206_java_2_3
|
Intégrer |
<iframe src="https://card2brain.ch/box/20201206_java_2_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
How to create a finite stream for a collection where the stream can run in parallel?
coll.parallelStream()
How to create a finite/infinite stream by using the seed for the first element and trhen calling the unaryOperator for each subsequent element upon request and stop if a predicate return false
Stream.iterate(seed, predicate, unaryOperator)
How to create a infinite stream by calling the supplier for each element upon request?
Stream.generate(supplier)
How to create a infinite stream by using the seed for the first element and then calling the UnaryOperator for each subsequent element upon request?
Stream.iterate(seed, unaryOperator)
Doest stream.count terminate for an infinite stream?
no
Does stream min/max terminate on an infinite stream?
no
What is returned by stream min/max?
Optional<T>
Is it predictable which element gets returned by findAny()?
no
Whats the returntype of findAny() / findFirst()?
Optional<T>
Whats the returntype of allMatch/anyMatch/noneMatch?
boolean
What is the only terminal operation with returntype void?
forEach()
Can streams be used as a source for a for-each-loop?
no, they dont implement the iterable interface
What does reduce do?
combines a stream into a single object
What are the three method signatures of reduce?
- T reduce(T identity, BinaryOperator<T> accumulator)
- Optional<T> reduce(BinaryOperator<T> accumulator)
- <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)
What is the result of reduce when the stream is empty?
empty optional
What is the result of reduce when the stream has one element?
the element
What is the result of reduce when the stream has multiple elements?
the accumulator is applied to combine them
When is the three-arguement reduce operation used?
when working with parallel streams because it allows the stream to be decomposed and reassembled by separate threads
What are the method-signatures of collect?
<R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R,R> combiner)
How to get a stream without duplicates in it? How are the duplicates determined?
distinct(), Java calls equals()
Stream<T> distinct()
How to make an infinite stream finite?
- Stream<T> limit(long maxSize)
- Stream<T> skip(long n)
When is flatMap useful?
when you want to remove empty elements from a stream or you want to combine a stream of lists
What are the signatures of sorted()?
- Stream<T> sorted()
- Stream<T> sorted(Comparator<? super T> comparator)
Does this work?
Stream<String> s = Stream.of("brown bear-", "grizzly-");
s.sorted(Comparator::reverseOrder);
No,, comparator is a functinal interface -> we can use method references/lambdas to implement it. It implements one methods that takse two string paramaters and returns an int. But Comparator::reverseOrder doesnt do that, its a reference to a function that takes zero parameters and returns Comparator
What are the three primitive streams?
IntStream
LongStream
DoubleStream
For which functions is the method valid and what is the method for the aritmetic mean of the elements?
OptionalDouble average()
IntStream, LongStream, DoubleStream
For which functions is the method valid and what is the method for a stream<T> where T is the wrapper class associated with the primitive value?
Stream<T> boxed()
IntStream, LongStream, DoubleStream
For which functions is the method valid and what is the method for the maximum element of the stream?
OptionalInt max()
OptionalLong max()
OptionalDouble max()
IntStream, LongStream, DoubleStream
For which functions is the method valid and what is the method for the minimum element of the stream?
OptionalInt/OptionalLong/OptionalDouble min()
IntStream/LongStream/DoubleStream
For which functions is the method valid and what is the method for returning a primitive stream from a (inclusive) a to a (exclusive) b?
IntStream/LongStream range(int/long a, int/long b)
IntStream/LongStream
For which functions is the method valid and what is the method for returining a primitive stream form (inclusive) a to (inclusive) b?
IntStream/LongStream rangeClosed(int/long a, int/long b)
IntStream/LongStream
For which functions is the method valid and what is the method for returning the sum of the elements in the stream?
int/long/double sum()
IntStream/LongStream/DoubleStream
For which functions is the method valid and what is the method for returning an object containing numerous stream statistics such as the average, min, max, etc?
IntSummaryStatistics/LongSummaryStatistics/DoubleSummaryStatistics summaryStatistics()
IntStream/LongStream/DoubleStream
Whats the diffierence between OptionalDouble and Optional<Double>?
OptionalDouble is for primitives and Optional<Doubloe> for the double wrapper class
What are the optionals for primitives?
OptionalDouble, OptionalInt, OptionalLong
How could this be rewritten?
private static void threeDigit(Optional<Integer> optional) {
if(optional.isPresent() {
var num = optional.get();
var string = "" + num;
if(string.length() == 3)
System.out.println(string);
}
}
private static void threeDigiht(Optional<Integer> optional) {
optional.map(n -> ""+n)
.filter(s -> s.length() == 3)
.ifPresent(System.out::println);
}
How to flat two optionals?
optional.flatMap
What is the Collector and whats the return value for calculationg the average for our three core primitive types?
Double averagingDouble(ToDoubleFunction f)
Double averagingInt(ToIntFunction f)
Double averagingLong(ToLongFunction f)
What is the Collector and whats the return value for counting the number of elements?
Long counting()
What is the Collector and whats the return value for creating a map grouping by the specified function with the optional map type spplier and optional downstream collector?
Map<K, List<T>> groupingBy(Function f)
Map<K, List<T>> groupingBy(Function f, Collector dc)
Map<K, List<T>> groupingBy(Function f, Supplier s, Collector dc)