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
Q

Stream.findFirst()

A

Returns an Optional describing the first 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
Q

How can we retrieve any element from a stream?

A

Stream.findAny()

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

How can we retrieve the first element of a stream?

A

Stream.findFirst()

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

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

A

Returns whether all elements of the stream match the provided predicate.

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

How can we check if all elements of a stream satisfy a given condition?

A

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

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

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

A

Returns whether any elements of the stream match the provided predicate.

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

How can we check if at least one element of a stream satisfies a given condition?

A

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

30
Q

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

A

Stream.count()

31
Q

Stream.count()

A

Returns the count of elements in the stream.

32
Q

Stream.min(Comparator<? super T> comparator)

A

Returns an Optional describing the minimum element of the stream according to the provided Comparator.

33
Q

How can we find the minimum element in a stream?

A

Stream.min(Comparator<? super T> comparator)

34
Q

Stream.max(Comparator<? super T> comparator)

A

Returns an Optional describing the maximum element of the stream according to the provided Comparator.

35
Q

How can we find the maximum element in a stream?

A

Stream.max(Comparator<? super T> comparator)

36
Q

Stream.reduce()

A

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
Q

How can we reduce the elements of a stream to a single value?

A

Stream.reduce(BinaryOperator<T> accumulator)</T>

38
Q

Stream.collect()

A

Performs a mutable reduction operation on the elements of the stream using a Collector.

39
Q

How can we perform a mutable reduction of the elements in a stream?

A

Stream.collect(Collector<? super T,A,R> collector)

40
Q

What is spliterator?

A

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<String> spliterator = list.spliterator();</String>

Then spliterator.trySplit() or spliterator.tryAdvance(), which gets next element in stream.

41
Q

Optional.empty()

A

Returns an empty Optional instance.

42
Q

How can we create an empty Optional?

A

Optional.empty()

43
Q

Optional.get()

A

Returns the value if present, otherwise throws NoSuchElementException.

44
Q

How can we retrieve the value from an Optional if it’s present?

A

Optional.get()

45
Q

Optional.isPresent()

A

Returns true if there is a value present, otherwise false.

46
Q

How can we check if a value is present in an Optional?

A

Optional.isPresent()

47
Q

Optional.of()

A

Returns an Optional with the specified present non-null value.

47
Q

Optional.orElse()

A

Returns the value if present, otherwise returns the provided default value.

47
Q

How can we create an Optional with a non-null value?

A

Optional.of(T value)

48
Q

How can we get the value from an Optional or a default if it’s not present?

A

Optional.orElse(T other)

49
Q

Optional.orElseGet()

A

Returns the value if present, otherwise invokes the provided supplier and returns its result.

50
Q

How can we get the value from an Optional or compute a default if it’s not present?

A

Optional.orElseGet(Supplier<? extends T> supplier)

51
Q

Optional.orElseThrow()

A

Returns the contained value, if present, otherwise throws an exception produced by the provided supplier.

52
Q

Collectors.summarizingDouble/Long/Int()

A

Produces a summary of statistics for numeric stream elements.

53
Q

How can we generate summary statistics for numeric stream elements?

A

Collectors.summarizingDouble/Long/Int(ToDoubleFunction/ToLongFunction/ToIntFunction)

54
Q

Collectors.toCollection()

A

Collects stream elements into a specific Collection type.

55
Q

How can we collect stream elements into a specific Collection type?

A

Collectors.toCollection(Supplier<C> collectionFactory)</C>

56
Q

Collectors.toList()

A

Collects stream elements into a List.

57
Q

Collectors.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

A

Collects stream elements into a Map.

58
Q

How can we collect stream elements into a List?

A

Collectors.toList()

58
Q

How can we collect stream elements into a Map?

A

Collectors.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

59
Q

Collectors.toSet()

A

Collects stream elements into a Set.

60
Q

How can we collect stream elements into a Set?

A

Collectors.toSet()

61
Q

Collectors.joining(CharSequence delimiter)

A

Concatenates stream elements into a String with an optional delimiter.

61
Q

How can we partition stream elements into a Map based on a condition?

A

Collectors.partitioningBy(Predicate<? super T> predicate)

61
Q

Collectors.partitioningBy(Predicate<? super T> predicate)

A

Partitions stream elements into a Map based on a Predicate.

62
Q

How can we concatenate stream elements into a String with a delimiter?

A

Collectors.joining(CharSequence delimiter)

63
Q

Collectors.groupingBy()

A

Groups stream elements into a Map based on a classification function.

64
Q

How can we group stream elements into a Map based on a classification function?

A

Collectors.groupingBy(Function<? super T, ? extends K> classifier)

65
Q

Collectors.teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)

A

Combines two collectors into a single one.

66
Q

How can we combine two collectors into a single collector?

A

Collectors.teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)

67
Q

IntStream, LongStream, DoubleStream?

A

Stream creating classes that create stream for primitives using range() and rangeClosed()

68
Q

How can we get max, min, average or sum using Streams?

A

Using IntStream, LongStream, DoubleStream

69
Q

How can we summarize statistics using Streams?

A

Calling summaryStatistics() on IntStream, LongStream, DoubleStream. Then we can getAverage(), getCount(), getMin(), getMax()

70
Q

How can we map to other objects from IntStream, LongStream, DoubleStream?

A

Calling map(), mapTo(), mapToObj()