From 7c658527094083b2037d362916adf8eb2493ea65 Mon Sep 17 00:00:00 2001
From: Gary Gregory This is an intermediate operation.
+ *
+ * This is an intermediate operation.
+ * This is a terminal operation.
+ *
+ * This is an intermediate operation.
+ * The behavior of this operation is explicitly nondeterministic.
+ *
+ * The behavior of this operation is explicitly nondeterministic.
* For parallel stream pipelines, this operation does not
* guarantee to respect the encounter order of the stream, as doing so
* would sacrifice the benefit of parallelism. For any given element, the
* action may be performed at whatever time and in whatever thread the
* library chooses. If the action accesses shared state, it is
* responsible for providing the required synchronization.
+ * If the underlying stream is parallel, and the {@code Collector}
+ *
+ * If the underlying stream is parallel, and the {@code Collector}
* is concurrent, and either the stream is unordered or the collector is
* unordered, then a concurrent reduction will be performed
* (see {@link Collector} for details on concurrent reduction.)
+ * This is a terminal operation.
+ *
+ * This is an intermediate operation.
+ * When executed in parallel, multiple intermediate results may be
+ *
+ * When executed in parallel, multiple intermediate results may be
* instantiated, populated, and merged so as to maintain isolation of
* mutable data structures. Therefore, even when executed in parallel
* with non-thread-safe data structures (such as {@code ArrayList}), no
* additional synchronization is needed for a parallel reduction.
- *
+ *
* Note
* The following will accumulate strings into an ArrayList:
+ * The following will classify {@code Person} objects by city:
+ *
+ * The following will classify {@code Person} objects by city:
+ * The following will classify {@code Person} objects by state and city,
+ *
+ * The following will classify {@code Person} objects by state and city,
* cascading two {@code Collector}s together:
+ * Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
+ *
+ * Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
* can be parallelized without requiring additional synchronization.
+ * This is a terminal operation.
+ *
+ * This is an intermediate operation.
+ *
* Note There are many existing classes in the JDK whose signatures are
* well-suited for use with method references as arguments to {@code collect()}.
* For example, the following will accumulate strings into an {@code ArrayList}:
+ * The following will take a stream of strings and concatenates them into a
+ *
+ * The following will take a stream of strings and concatenates them into a
* single string:
+ * The {@code identity} value must be an identity for the accumulator
+ *
+ * The {@code identity} value must be an identity for the accumulator
* function. This means that for all {@code t},
* {@code accumulator.apply(identity, t)} is equal to {@code t}.
* The {@code accumulator} function must be an associative function.
+ * This is a terminal operation.
+ *
+ * This is an intermediate operation.
+ * While this may seem a more roundabout way to perform an aggregation
+ *
+ * While this may seem a more roundabout way to perform an aggregation
* compared to simply mutating a running total in a loop, reduction
* operations parallelize more gracefully, without needing additional
* synchronization and with greatly reduced risk of data races.
+ * This is an intermediate operation.
+ *
+ * This is an intermediate operation.
+ * This is a short-circuiting terminal operation.
+ *
+ * This is a short-circuiting terminal operation.
+ *
* Note
* This method evaluates the universal quantification of the
* predicate over the elements of the stream (for all x P(x)). If the
* stream is empty, the quantification is said to be vacuously
* satisfied and is always {@code true} (regardless of P(x)).
+ * This is a short-circuiting terminal operation.
+ *
+ * This is a short-circuiting terminal operation.
+ *
* Using a {@link FailableStream}, this can be rewritten as follows:
+ *
* This is a short-circuiting terminal operation.
+ *
* This is a short-circuiting terminal operation.
+ * {@code
* List
*
- * {@code
* Map
*
- * {@code
* Map
*
- * {@code
* List
*
- * {@code
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
* StringBuilder::append)
@@ -249,12 +274,16 @@ public class Streams {
*
* but is not constrained to execute sequentially.
*
- *
*
- *
* Consumer<java.lang.reflect.Method> consumer = m -> {
* try {
@@ -54,7 +54,9 @@ import org.apache.commons.lang3.function.FailablePredicate;
* };
* stream.forEach(consumer);
*
+ *
* Streams.failable(stream).forEach((m) -> m.invoke(o, args));
*
@@ -140,6 +142,7 @@ public class Streams {
*
*
* This is a terminal operation. + *
* ** When executed in parallel, multiple intermediate results may be instantiated, populated, and merged so as to * maintain isolation of mutable data structures. Therefore, even when executed in parallel with non-thread-safe * data structures (such as {@code ArrayList}), no additional synchronization is needed for a parallel * reduction. + *
* * Note The following will accumulate strings into an ArrayList: * @@ -214,6 +221,7 @@ public class Streams { * ** The following will classify {@code Person} objects by city: + *
* ** {@code @@ -224,6 +232,7 @@ public class Streams { ** The following will classify {@code Person} objects by state and city, cascading two {@code Collector}s * together: + *
* ** {@code @@ -262,9 +271,11 @@ public class Streams { ** Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations can be parallelized without * requiring additional synchronization. + *
* ** This is a terminal operation. + *
* * Note There are many existing classes in the JDK whose signatures are well-suited for use with method * references as arguments to {@code collect()}. For example, the following will accumulate strings into an @@ -278,6 +289,7 @@ public class Streams { * ** The following will take a stream of strings and concatenates them into a single string: + *
* ** {@code @@ -307,6 +319,7 @@ public class Streams { * ** This is an intermediate operation. + *
* * @param predicate a non-interfering, stateless predicate to apply to each element to determine if it should be * included. @@ -323,6 +336,7 @@ public class Streams { * ** This is a terminal operation. + *
* ** The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation @@ -330,6 +344,7 @@ public class Streams { * benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever * thread the library chooses. If the action accesses shared state, it is responsible for providing the required * synchronization. + *
* * @param action a non-interfering action to perform on the elements */ @@ -353,6 +368,7 @@ public class Streams { * ** This is an intermediate operation. + *
* * @paramThe element type of the new stream * @param mapper A non-interfering, stateless function to apply to each element @@ -382,9 +398,11 @@ public class Streams { * The {@code identity} value must be an identity for the accumulator function. This means that for all * {@code t}, {@code accumulator.apply(identity, t)} is equal to {@code t}. The {@code accumulator} function * must be an associative function. + * * * * This is a terminal operation. + *
* * Note Sum, min, max, average, and string concatenation are all special cases of reduction. Summing a * stream of numbers can be expressed as: @@ -407,6 +425,7 @@ public class Streams { * While this may seem a more roundabout way to perform an aggregation compared to simply mutating a running * total in a loop, reduction operations parallelize more gracefully, without needing additional synchronization * and with greatly reduced risk of data races. + * * * @param identity the identity value for the accumulating function * @param accumulator an associative, non-interfering, stateless function for combining two values