jdk/src/java.base/share/classes/java/util/stream/package-info.java
changeset 44844 b2b4d98404ba
parent 38442 387a4457880c
child 47004 b7e72fc752c9
equal deleted inserted replaced
44843:c1b0ca015127 44844:b2b4d98404ba
    92  * </ul>
    92  * </ul>
    93  *
    93  *
    94  * <p>Additional stream sources can be provided by third-party libraries using
    94  * <p>Additional stream sources can be provided by third-party libraries using
    95  * <a href="package-summary.html#StreamSources">these techniques</a>.
    95  * <a href="package-summary.html#StreamSources">these techniques</a>.
    96  *
    96  *
    97  * <h2><a name="StreamOps">Stream operations and pipelines</a></h2>
    97  * <h2><a id="StreamOps">Stream operations and pipelines</a></h2>
    98  *
    98  *
    99  * <p>Stream operations are divided into <em>intermediate</em> and
    99  * <p>Stream operations are divided into <em>intermediate</em> and
   100  * <em>terminal</em> operations, and are combined to form <em>stream
   100  * <em>terminal</em> operations, and are combined to form <em>stream
   101  * pipelines</em>.  A stream pipeline consists of a source (such as a
   101  * pipelines</em>.  A stream pipeline consists of a source (such as a
   102  * {@code Collection}, an array, a generator function, or an I/O channel);
   102  * {@code Collection}, an array, a generator function, or an I/O channel);
   157  * operation is short-circuiting if, when presented with infinite input, it may
   157  * operation is short-circuiting if, when presented with infinite input, it may
   158  * terminate in finite time.  Having a short-circuiting operation in the pipeline
   158  * terminate in finite time.  Having a short-circuiting operation in the pipeline
   159  * is a necessary, but not sufficient, condition for the processing of an infinite
   159  * is a necessary, but not sufficient, condition for the processing of an infinite
   160  * stream to terminate normally in finite time.
   160  * stream to terminate normally in finite time.
   161  *
   161  *
   162  * <h3><a name="Parallelism">Parallelism</a></h3>
   162  * <h3><a id="Parallelism">Parallelism</a></h3>
   163  *
   163  *
   164  * <p>Processing elements with an explicit {@code for-}loop is inherently serial.
   164  * <p>Processing elements with an explicit {@code for-}loop is inherently serial.
   165  * Streams facilitate parallel execution by reframing the computation as a pipeline of
   165  * Streams facilitate parallel execution by reframing the computation as a pipeline of
   166  * aggregate operations, rather than as imperative operations on each individual
   166  * aggregate operations, rather than as imperative operations on each individual
   167  * element.  All streams operations can execute either in serial or in parallel.
   167  * element.  All streams operations can execute either in serial or in parallel.
   204  * most cases must be <em>stateless</em>.  Such parameters are always instances
   204  * most cases must be <em>stateless</em>.  Such parameters are always instances
   205  * of a <a href="../function/package-summary.html">functional interface</a> such
   205  * of a <a href="../function/package-summary.html">functional interface</a> such
   206  * as {@link java.util.function.Function}, and are often lambda expressions or
   206  * as {@link java.util.function.Function}, and are often lambda expressions or
   207  * method references.
   207  * method references.
   208  *
   208  *
   209  * <h3><a name="NonInterference">Non-interference</a></h3>
   209  * <h3><a id="NonInterference">Non-interference</a></h3>
   210  *
   210  *
   211  * Streams enable you to execute possibly-parallel aggregate operations over a
   211  * Streams enable you to execute possibly-parallel aggregate operations over a
   212  * variety of data sources, including even non-thread-safe collections such as
   212  * variety of data sources, including even non-thread-safe collections such as
   213  * {@code ArrayList}. This is possible only if we can prevent
   213  * {@code ArrayList}. This is possible only if we can prevent
   214  * <em>interference</em> with the data source during the execution of a stream
   214  * <em>interference</em> with the data source during the execution of a stream
   250  * streams returned from JDK collections, and most other JDK classes,
   250  * streams returned from JDK collections, and most other JDK classes,
   251  * are well-behaved in this manner; for streams generated by other libraries, see
   251  * are well-behaved in this manner; for streams generated by other libraries, see
   252  * <a href="package-summary.html#StreamSources">Low-level stream
   252  * <a href="package-summary.html#StreamSources">Low-level stream
   253  * construction</a> for requirements for building well-behaved streams.
   253  * construction</a> for requirements for building well-behaved streams.
   254  *
   254  *
   255  * <h3><a name="Statelessness">Stateless behaviors</a></h3>
   255  * <h3><a id="Statelessness">Stateless behaviors</a></h3>
   256  *
   256  *
   257  * Stream pipeline results may be nondeterministic or incorrect if the behavioral
   257  * Stream pipeline results may be nondeterministic or incorrect if the behavioral
   258  * parameters to the stream operations are <em>stateful</em>.  A stateful lambda
   258  * parameters to the stream operations are <em>stateful</em>.  A stateful lambda
   259  * (or other object implementing the appropriate functional interface) is one
   259  * (or other object implementing the appropriate functional interface) is one
   260  * whose result depends on any state which might change during the execution
   260  * whose result depends on any state which might change during the execution
   278  * state, you risk having contention undermine the parallelism you are seeking
   278  * state, you risk having contention undermine the parallelism you are seeking
   279  * to benefit from.  The best approach is to avoid stateful behavioral
   279  * to benefit from.  The best approach is to avoid stateful behavioral
   280  * parameters to stream operations entirely; there is usually a way to
   280  * parameters to stream operations entirely; there is usually a way to
   281  * restructure the stream pipeline to avoid statefulness.
   281  * restructure the stream pipeline to avoid statefulness.
   282  *
   282  *
   283  * <h3><a name="SideEffects">Side-effects</a></h3>
   283  * <h3><a id="SideEffects">Side-effects</a></h3>
   284  *
   284  *
   285  * Side-effects in behavioral parameters to stream operations are, in general,
   285  * Side-effects in behavioral parameters to stream operations are, in general,
   286  * discouraged, as they can often lead to unwitting violations of the
   286  * discouraged, as they can often lead to unwitting violations of the
   287  * statelessness requirement, as well as other thread-safety hazards.
   287  * statelessness requirement, as well as other thread-safety hazards.
   288  *
   288  *
   347  *     List<String>results =
   347  *     List<String>results =
   348  *         stream.filter(s -> pattern.matcher(s).matches())
   348  *         stream.filter(s -> pattern.matcher(s).matches())
   349  *               .collect(Collectors.toList());  // No side-effects!
   349  *               .collect(Collectors.toList());  // No side-effects!
   350  * }</pre>
   350  * }</pre>
   351  *
   351  *
   352  * <h3><a name="Ordering">Ordering</a></h3>
   352  * <h3><a id="Ordering">Ordering</a></h3>
   353  *
   353  *
   354  * <p>Streams may or may not have a defined <em>encounter order</em>.  Whether
   354  * <p>Streams may or may not have a defined <em>encounter order</em>.  Whether
   355  * or not a stream has an encounter order depends on the source and the
   355  * or not a stream has an encounter order depends on the source and the
   356  * intermediate operations.  Certain stream sources (such as {@code List} or
   356  * intermediate operations.  Certain stream sources (such as {@code List} or
   357  * arrays) are intrinsically ordered, whereas others (such as {@code HashSet})
   357  * arrays) are intrinsically ordered, whereas others (such as {@code HashSet})
   386  * the stream with {@link java.util.stream.BaseStream#unordered() unordered()} may
   386  * the stream with {@link java.util.stream.BaseStream#unordered() unordered()} may
   387  * improve parallel performance for some stateful or terminal operations.
   387  * improve parallel performance for some stateful or terminal operations.
   388  * However, most stream pipelines, such as the "sum of weight of blocks" example
   388  * However, most stream pipelines, such as the "sum of weight of blocks" example
   389  * above, still parallelize efficiently even under ordering constraints.
   389  * above, still parallelize efficiently even under ordering constraints.
   390  *
   390  *
   391  * <h2><a name="Reduction">Reduction operations</a></h2>
   391  * <h2><a id="Reduction">Reduction operations</a></h2>
   392  *
   392  *
   393  * A <em>reduction</em> operation (also called a <em>fold</em>) takes a sequence
   393  * A <em>reduction</em> operation (also called a <em>fold</em>) takes a sequence
   394  * of input elements and combines them into a single summary result by repeated
   394  * of input elements and combines them into a single summary result by repeated
   395  * application of a combining operation, such as finding the sum or maximum of
   395  * application of a combining operation, such as finding the sum or maximum of
   396  * a set of numbers, or accumulating elements into a list.  The streams classes have
   396  * a set of numbers, or accumulating elements into a list.  The streams classes have
   491  * though the explicit map-reduce form is more readable and therefore should
   491  * though the explicit map-reduce form is more readable and therefore should
   492  * usually be preferred. The generalized form is provided for cases where
   492  * usually be preferred. The generalized form is provided for cases where
   493  * significant work can be optimized away by combining mapping and reducing
   493  * significant work can be optimized away by combining mapping and reducing
   494  * into a single function.
   494  * into a single function.
   495  *
   495  *
   496  * <h3><a name="MutableReduction">Mutable reduction</a></h3>
   496  * <h3><a id="MutableReduction">Mutable reduction</a></h3>
   497  *
   497  *
   498  * A <em>mutable reduction operation</em> accumulates input elements into a
   498  * A <em>mutable reduction operation</em> accumulates input elements into a
   499  * mutable result container, such as a {@code Collection} or {@code StringBuilder},
   499  * mutable result container, such as a {@code Collection} or {@code StringBuilder},
   500  * as it processes the elements in the stream.
   500  * as it processes the elements in the stream.
   501  *
   501  *
   618  *
   618  *
   619  * <p>Here, equivalence generally means according to {@link java.lang.Object#equals(Object)}.
   619  * <p>Here, equivalence generally means according to {@link java.lang.Object#equals(Object)}.
   620  * but in some cases equivalence may be relaxed to account for differences in
   620  * but in some cases equivalence may be relaxed to account for differences in
   621  * order.
   621  * order.
   622  *
   622  *
   623  * <h3><a name="ConcurrentReduction">Reduction, concurrency, and ordering</a></h3>
   623  * <h3><a id="ConcurrentReduction">Reduction, concurrency, and ordering</a></h3>
   624  *
   624  *
   625  * With some complex reduction operations, for example a {@code collect()} that
   625  * With some complex reduction operations, for example a {@code collect()} that
   626  * produces a {@code Map}, such as:
   626  * produces a {@code Map}, such as:
   627  * <pre>{@code
   627  * <pre>{@code
   628  *     Map<Buyer, List<Transaction>> salesByBuyer
   628  *     Map<Buyer, List<Transaction>> salesByBuyer
   673  * the order they appear in the source, then we cannot use a concurrent
   673  * the order they appear in the source, then we cannot use a concurrent
   674  * reduction, as ordering is one of the casualties of concurrent insertion.
   674  * reduction, as ordering is one of the casualties of concurrent insertion.
   675  * We would then be constrained to implement either a sequential reduction or
   675  * We would then be constrained to implement either a sequential reduction or
   676  * a merge-based parallel reduction.
   676  * a merge-based parallel reduction.
   677  *
   677  *
   678  * <h3><a name="Associativity">Associativity</a></h3>
   678  * <h3><a id="Associativity">Associativity</a></h3>
   679  *
   679  *
   680  * An operator or function {@code op} is <em>associative</em> if the following
   680  * An operator or function {@code op} is <em>associative</em> if the following
   681  * holds:
   681  * holds:
   682  * <pre>{@code
   682  * <pre>{@code
   683  *     (a op b) op c == a op (b op c)
   683  *     (a op b) op c == a op (b op c)
   691  * then invoke {@code op} on the results.
   691  * then invoke {@code op} on the results.
   692  *
   692  *
   693  * <p>Examples of associative operations include numeric addition, min, and
   693  * <p>Examples of associative operations include numeric addition, min, and
   694  * max, and string concatenation.
   694  * max, and string concatenation.
   695  *
   695  *
   696  * <h2><a name="StreamSources">Low-level stream construction</a></h2>
   696  * <h2><a id="StreamSources">Low-level stream construction</a></h2>
   697  *
   697  *
   698  * So far, all the stream examples have used methods like
   698  * So far, all the stream examples have used methods like
   699  * {@link java.util.Collection#stream()} or {@link java.util.Arrays#stream(Object[])}
   699  * {@link java.util.Collection#stream()} or {@link java.util.Arrays#stream(Object[])}
   700  * to obtain a stream.  How are those stream-bearing methods implemented?
   700  * to obtain a stream.  How are those stream-bearing methods implemented?
   701  *
   701  *