Streams Flashcards
What types of streams are there?
Finite and infinite
How do we start finite streams?
Collection.stream()
Stream.of()
Stream.empty()
How do we start infinite streams?
Stream.generate()
Stream.iterate(start, iterator)
Stream.iterate(start, end, iterator) -> can be finite
Stream.filter(Predicate<? super T> predicate)
Filters elements of the stream based on a given predicate, keeping only those that satisfy the condition.
How can we filter elements of the stream based on a given predicate?
Stream.filter(Predicate<? super T> predicate)
Stream.distinct()
Returns a stream with distinct elements (removes duplicates).
How can we remove duplicate elements from a stream?
Stream.distinct()
Stream.limit(long maxSize)
Truncates the stream to be no longer than the given maximum size.
How can we limit the number of elements in a stream?
Stream.limit(long maxSize)
Stream.skip()
Returns a stream that skips the first n elements.
How can we skip the first n elements of a stream?
Stream.skip(long n)
Stream.map()
Transforms each element of the stream using the provided function.
How can we transform each element of a stream using a function?
Stream.map(Function<? super T, ? extends R> mapper)
Stream.flatMap()
Transforms each element into a stream of new elements and then flattens these streams into a single stream.
How can we transform each element into a stream and flatten the result?
Stream.flatMap(Function<? super T, ? extends Stream<? extends R» mapper)
Stream.sorted()
Returns a stream with elements sorted according to natural order or a provided comparator. Can hang app if used with infinite stream.
How can we sort the elements of a stream?
Stream.sorted() or Stream.sorted(Comparator<? super T> comparator)
Stream.peek()
Performs an action on each element of the stream without modifying it, useful for debugging.
How can we perform an action on each element of a stream without modifying it?
Stream.peek(Consumer<? super T> action)
What are intermediate streams?
filter, distinct, peek, skip, limit, map, flatMap, sorted.
They are intermediate because they are between starting operations and terminal operations.
Name some terminal streams?
findAny, findFirst, allMatch, anyMatch, count, min, max, reduce, collect.
They are terminal because they close stream after they finish.
Stream.forEach()
Performs an action for each element of the stream.
How can we perform an action for each element of a stream?
Stream.forEach(Consumer<? super T> action)
Stream.findAny()
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
Stream.findFirst()
Returns an Optional describing the first element of the stream, or an empty Optional if the stream is empty.
How can we retrieve any element from a stream?
Stream.findAny()
How can we retrieve the first element of a stream?
Stream.findFirst()
Stream.allMatch(Predicate<? super T> predicate)
Returns whether all elements of the stream match the provided predicate.
How can we check if all elements of a stream satisfy a given condition?
Stream.allMatch(Predicate<? super T> predicate)
Stream.anyMatch(Predicate<? super T> predicate)
Returns whether any elements of the stream match the provided predicate.