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.
How can we check if at least one element of a stream satisfies a given condition?
Stream.anyMatch(Predicate<? super T> predicate)
How can we count the number of elements in a stream?
Stream.count()
Stream.count()
Returns the count of elements in the stream.
Stream.min(Comparator<? super T> comparator)
Returns an Optional describing the minimum element of the stream according to the provided Comparator.
How can we find the minimum element in a stream?
Stream.min(Comparator<? super T> comparator)
Stream.max(Comparator<? super T> comparator)
Returns an Optional describing the maximum element of the stream according to the provided Comparator.
How can we find the maximum element in a stream?
Stream.max(Comparator<? super T> comparator)
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.
How can we reduce the elements of a stream to a single value?
Stream.reduce(BinaryOperator<T> accumulator)</T>
Stream.collect()
Performs a mutable reduction operation on the elements of the stream using a Collector.
How can we perform a mutable reduction of the elements in a stream?
Stream.collect(Collector<? super T,A,R> collector)
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<String> spliterator = list.spliterator();</String>
Then spliterator.trySplit() or spliterator.tryAdvance(), which gets next element in stream.
Optional.empty()
Returns an empty Optional instance.
How can we create an empty Optional?
Optional.empty()
Optional.get()
Returns the value if present, otherwise throws NoSuchElementException.
How can we retrieve the value from an Optional if it’s present?
Optional.get()
Optional.isPresent()
Returns true if there is a value present, otherwise false.
How can we check if a value is present in an Optional?
Optional.isPresent()
Optional.of()
Returns an Optional with the specified present non-null value.
Optional.orElse()
Returns the value if present, otherwise returns the provided default value.
How can we create an Optional with a non-null value?
Optional.of(T value)
How can we get the value from an Optional or a default if it’s not present?
Optional.orElse(T other)
Optional.orElseGet()
Returns the value if present, otherwise invokes the provided supplier and returns its result.
How can we get the value from an Optional or compute a default if it’s not present?
Optional.orElseGet(Supplier<? extends T> supplier)
Optional.orElseThrow()
Returns the contained value, if present, otherwise throws an exception produced by the provided supplier.
Collectors.summarizingDouble/Long/Int()
Produces a summary of statistics for numeric stream elements.
How can we generate summary statistics for numeric stream elements?
Collectors.summarizingDouble/Long/Int(ToDoubleFunction/ToLongFunction/ToIntFunction)
Collectors.toCollection()
Collects stream elements into a specific Collection type.
How can we collect stream elements into a specific Collection type?
Collectors.toCollection(Supplier<C> collectionFactory)</C>
Collectors.toList()
Collects stream elements into a List.
Collectors.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
Collects stream elements into a Map.
How can we collect stream elements into a List?
Collectors.toList()
How can we collect stream elements into a Map?
Collectors.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
Collectors.toSet()
Collects stream elements into a Set.
How can we collect stream elements into a Set?
Collectors.toSet()
Collectors.joining(CharSequence delimiter)
Concatenates stream elements into a String with an optional delimiter.
How can we partition stream elements into a Map based on a condition?
Collectors.partitioningBy(Predicate<? super T> predicate)
Collectors.partitioningBy(Predicate<? super T> predicate)
Partitions stream elements into a Map based on a Predicate.
How can we concatenate stream elements into a String with a delimiter?
Collectors.joining(CharSequence delimiter)
Collectors.groupingBy()
Groups stream elements into a Map based on a classification function.
How can we group stream elements into a Map based on a classification function?
Collectors.groupingBy(Function<? super T, ? extends K> classifier)
Collectors.teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)
Combines two collectors into a single one.
How can we combine two collectors into a single collector?
Collectors.teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)
IntStream, LongStream, DoubleStream?
Stream creating classes that create stream for primitives using range() and rangeClosed()
How can we get max, min, average or sum using Streams?
Using IntStream, LongStream, DoubleStream
How can we summarize statistics using Streams?
Calling summaryStatistics() on IntStream, LongStream, DoubleStream. Then we can getAverage(), getCount(), getMin(), getMax()
How can we map to other objects from IntStream, LongStream, DoubleStream?
Calling map(), mapTo(), mapToObj()