Streams Flashcards

1
Q

What types of streams are there?

A

Finite and infinite

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do we start finite streams?

A

Collection.stream()
Stream.of()
Stream.empty()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we start infinite streams?

A

Stream.generate()
Stream.iterate(start, iterator)
Stream.iterate(start, end, iterator) -> can be finite

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Stream.filter(Predicate<? super T> predicate)

A

Filters elements of the stream based on a given predicate, keeping only those that satisfy the condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we filter elements of the stream based on a given predicate?

A

Stream.filter(Predicate<? super T> predicate)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Stream.distinct()

A

Returns a stream with distinct elements (removes duplicates).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we remove duplicate elements from a stream?

A

Stream.distinct()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stream.limit(long maxSize)

A

Truncates the stream to be no longer than the given maximum size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we limit the number of elements in a stream?

A

Stream.limit(long maxSize)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Stream.skip()

A

Returns a stream that skips the first n elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we skip the first n elements of a stream?

A

Stream.skip(long n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Stream.map()

A

Transforms each element of the stream using the provided function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we transform each element of a stream using a function?

A

Stream.map(Function<? super T, ? extends R> mapper)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Stream.flatMap()

A

Transforms each element into a stream of new elements and then flattens these streams into a single stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we transform each element into a stream and flatten the result?

A

Stream.flatMap(Function<? super T, ? extends Stream<? extends R» mapper)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Stream.sorted()

A

Returns a stream with elements sorted according to natural order or a provided comparator. Can hang app if used with infinite stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can we sort the elements of a stream?

A

Stream.sorted() or Stream.sorted(Comparator<? super T> comparator)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Stream.peek()

A

Performs an action on each element of the stream without modifying it, useful for debugging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How can we perform an action on each element of a stream without modifying it?

A

Stream.peek(Consumer<? super T> action)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are intermediate streams?

A

filter, distinct, peek, skip, limit, map, flatMap, sorted.
They are intermediate because they are between starting operations and terminal operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Name some terminal streams?

A

findAny, findFirst, allMatch, anyMatch, count, min, max, reduce, collect.
They are terminal because they close stream after they finish.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Stream.forEach()

A

Performs an action for each element of the stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How can we perform an action for each element of a stream?

A

Stream.forEach(Consumer<? super T> action)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Stream.findAny()

A

Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Stream.findFirst()
Returns an Optional describing the first element of the stream, or an empty Optional if the stream is empty.
25
How can we retrieve any element from a stream?
Stream.findAny()
26
How can we retrieve the first element of a stream?
Stream.findFirst()
27
Stream.allMatch(Predicate predicate)
Returns whether all elements of the stream match the provided predicate.
28
How can we check if all elements of a stream satisfy a given condition?
Stream.allMatch(Predicate predicate)
29
Stream.anyMatch(Predicate predicate)
Returns whether any elements of the stream match the provided predicate.
30
How can we check if at least one element of a stream satisfies a given condition?
Stream.anyMatch(Predicate predicate)
30
How can we count the number of elements in a stream?
Stream.count()
31
Stream.count()
Returns the count of elements in the stream.
32
Stream.min(Comparator comparator)
Returns an Optional describing the minimum element of the stream according to the provided Comparator.
33
How can we find the minimum element in a stream?
Stream.min(Comparator comparator)
34
Stream.max(Comparator comparator)
Returns an Optional describing the maximum element of the stream according to the provided Comparator.
35
How can we find the maximum element in a stream?
Stream.max(Comparator comparator)
36
Stream.reduce()
Performs a reduction on the elements of the stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
37
How can we reduce the elements of a stream to a single value?
Stream.reduce(BinaryOperator accumulator)
38
Stream.collect()
Performs a mutable reduction operation on the elements of the stream using a Collector.
39
How can we perform a mutable reduction of the elements in a stream?
Stream.collect(Collector collector)
40
What is spliterator?
It is util that is used to try and ideally split stream and then return one half. It detects infinite streams so app cannot hang using it. We use spliterator like: Spliterator spliterator = list.spliterator(); Then spliterator.trySplit() or spliterator.tryAdvance(), which gets next element in stream.
41
Optional.empty()
Returns an empty Optional instance.
42
How can we create an empty Optional?
Optional.empty()
43
Optional.get()
Returns the value if present, otherwise throws NoSuchElementException.
44
How can we retrieve the value from an Optional if it's present?
Optional.get()
45
Optional.isPresent()
Returns true if there is a value present, otherwise false.
46
How can we check if a value is present in an Optional?
Optional.isPresent()
47
Optional.of()
Returns an Optional with the specified present non-null value.
47
Optional.orElse()
Returns the value if present, otherwise returns the provided default value.
47
How can we create an Optional with a non-null value?
Optional.of(T value)
48
How can we get the value from an Optional or a default if it's not present?
Optional.orElse(T other)
49
Optional.orElseGet()
Returns the value if present, otherwise invokes the provided supplier and returns its result.
50
How can we get the value from an Optional or compute a default if it's not present?
Optional.orElseGet(Supplier supplier)
51
Optional.orElseThrow()
Returns the contained value, if present, otherwise throws an exception produced by the provided supplier.
52
Collectors.summarizingDouble/Long/Int()
Produces a summary of statistics for numeric stream elements.
53
How can we generate summary statistics for numeric stream elements?
Collectors.summarizingDouble/Long/Int(ToDoubleFunction/ToLongFunction/ToIntFunction)
54
Collectors.toCollection()
Collects stream elements into a specific Collection type.
55
How can we collect stream elements into a specific Collection type?
Collectors.toCollection(Supplier collectionFactory)
56
Collectors.toList()
Collects stream elements into a List.
57
Collectors.toMap(Function keyMapper, Function valueMapper)
Collects stream elements into a Map.
58
How can we collect stream elements into a List?
Collectors.toList()
58
How can we collect stream elements into a Map?
Collectors.toMap(Function keyMapper, Function valueMapper)
59
Collectors.toSet()
Collects stream elements into a Set.
60
How can we collect stream elements into a Set?
Collectors.toSet()
61
Collectors.joining(CharSequence delimiter)
Concatenates stream elements into a String with an optional delimiter.
61
How can we partition stream elements into a Map based on a condition?
Collectors.partitioningBy(Predicate predicate)
61
Collectors.partitioningBy(Predicate predicate)
Partitions stream elements into a Map based on a Predicate.
62
How can we concatenate stream elements into a String with a delimiter?
Collectors.joining(CharSequence delimiter)
63
Collectors.groupingBy()
Groups stream elements into a Map based on a classification function.
64
How can we group stream elements into a Map based on a classification function?
Collectors.groupingBy(Function classifier)
65
Collectors.teeing(Collector downstream1, Collector downstream2, BiFunction merger)
Combines two collectors into a single one.
66
How can we combine two collectors into a single collector?
Collectors.teeing(Collector downstream1, Collector downstream2, BiFunction merger)
67
IntStream, LongStream, DoubleStream?
Stream creating classes that create stream for primitives using range() and rangeClosed()
68
How can we get max, min, average or sum using Streams?
Using IntStream, LongStream, DoubleStream
69
How can we summarize statistics using Streams?
Calling summaryStatistics() on IntStream, LongStream, DoubleStream. Then we can getAverage(), getCount(), getMin(), getMax()
70
How can we map to other objects from IntStream, LongStream, DoubleStream?
Calling map(), mapTo(), mapToObj()