jdk/src/java.base/share/classes/java/util/stream/Stream.java
author psandoz
Fri, 20 May 2016 11:47:39 +0200
changeset 38442 387a4457880c
parent 36223 de2976b3614c
child 38444 a5cdecb7d181
permissions -rw-r--r--
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline Reviewed-by: briangoetz, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     1
/*
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     4
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    10
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    16
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    20
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    23
 * questions.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    24
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    25
package java.util.stream;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    26
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    27
import java.nio.file.Files;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    28
import java.nio.file.Path;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    29
import java.util.Arrays;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    30
import java.util.Collection;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    31
import java.util.Comparator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    32
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    33
import java.util.Optional;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    34
import java.util.Spliterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    35
import java.util.Spliterators;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    36
import java.util.concurrent.ConcurrentHashMap;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    38
import java.util.function.BiFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    39
import java.util.function.BinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
import java.util.function.Consumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    41
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
import java.util.function.IntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
import java.util.function.Predicate;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    45
import java.util.function.ToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    46
import java.util.function.ToIntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    47
import java.util.function.ToLongFunction;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    48
import java.util.function.UnaryOperator;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    49
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    50
/**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    51
 * A sequence of elements supporting sequential and parallel aggregate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    52
 * operations.  The following example illustrates an aggregate operation using
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    53
 * {@link Stream} and {@link IntStream}:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    54
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    55
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    56
 *     int sum = widgets.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    57
 *                      .filter(w -> w.getColor() == RED)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    58
 *                      .mapToInt(w -> w.getWeight())
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    59
 *                      .sum();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    60
 * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    61
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    62
 * In this example, {@code widgets} is a {@code Collection<Widget>}.  We create
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    63
 * a stream of {@code Widget} objects via {@link Collection#stream Collection.stream()},
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    64
 * filter it to produce a stream containing only the red widgets, and then
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    65
 * transform it into a stream of {@code int} values representing the weight of
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    66
 * each red widget. Then this stream is summed to produce a total weight.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    67
 *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    68
 * <p>In addition to {@code Stream}, which is a stream of object references,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    69
 * there are primitive specializations for {@link IntStream}, {@link LongStream},
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    70
 * and {@link DoubleStream}, all of which are referred to as "streams" and
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    71
 * conform to the characteristics and restrictions described here.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    72
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    73
 * <p>To perform a computation, stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    74
 * <a href="package-summary.html#StreamOps">operations</a> are composed into a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    75
 * <em>stream pipeline</em>.  A stream pipeline consists of a source (which
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    76
 * might be an array, a collection, a generator function, an I/O channel,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    77
 * etc), zero or more <em>intermediate operations</em> (which transform a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    78
 * stream into another stream, such as {@link Stream#filter(Predicate)}), and a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    79
 * <em>terminal operation</em> (which produces a result or side-effect, such
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    80
 * as {@link Stream#count()} or {@link Stream#forEach(Consumer)}).
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    81
 * Streams are lazy; computation on the source data is only performed when the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    82
 * terminal operation is initiated, and source elements are consumed only
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    83
 * as needed.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    84
 *
38442
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    85
 * <p>A stream implementation is permitted significant latitude in optimizing
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    86
 * the computation of the result.  For example, a stream implementation is free
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    87
 * to elide operations (or entire stages) from a stream pipeline -- and
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    88
 * therefore elide invocation of behavioral parameters -- if it can prove that
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    89
 * it would not affect the result of the computation.  This means that
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    90
 * side-effects of behavioral parameters may not always be executed and should
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    91
 * not be relied upon, unless otherwise specified (such as by the terminal
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    92
 * operations {@code forEach} and {@code forEachOrdered}). (For a specific
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    93
 * example of such an optimization, see the API note documented on the
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    94
 * {@link #count} operation.  For more detail, see the
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    95
 * <a href="package-summary.html#SideEffects">side-effects</a> section of the
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    96
 * strean package documentation.)
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
    97
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    98
 * <p>Collections and streams, while bearing some superficial similarities,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    99
 * have different goals.  Collections are primarily concerned with the efficient
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   100
 * management of, and access to, their elements.  By contrast, streams do not
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   101
 * provide a means to directly access or manipulate their elements, and are
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   102
 * instead concerned with declaratively describing their source and the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   103
 * computational operations which will be performed in aggregate on that source.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   104
 * However, if the provided stream operations do not offer the desired
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   105
 * functionality, the {@link #iterator()} and {@link #spliterator()} operations
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   106
 * can be used to perform a controlled traversal.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   107
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   108
 * <p>A stream pipeline, like the "widgets" example above, can be viewed as
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   109
 * a <em>query</em> on the stream source.  Unless the source was explicitly
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   110
 * designed for concurrent modification (such as a {@link ConcurrentHashMap}),
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   111
 * unpredictable or erroneous behavior may result from modifying the stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   112
 * source while it is being queried.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   113
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   114
 * <p>Most stream operations accept parameters that describe user-specified
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   115
 * behavior, such as the lambda expression {@code w -> w.getWeight()} passed to
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   116
 * {@code mapToInt} in the example above.  To preserve correct behavior,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   117
 * these <em>behavioral parameters</em>:
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   118
 * <ul>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   119
 * <li>must be <a href="package-summary.html#NonInterference">non-interfering</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   120
 * (they do not modify the stream source); and</li>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   121
 * <li>in most cases must be <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   122
 * (their result should not depend on any state that might change during execution
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   123
 * of the stream pipeline).</li>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   124
 * </ul>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   125
 *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   126
 * <p>Such parameters are always instances of a
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   127
 * <a href="../function/package-summary.html">functional interface</a> such
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   128
 * as {@link java.util.function.Function}, and are often lambda expressions or
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   129
 * method references.  Unless otherwise specified these parameters must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   130
 * <em>non-null</em>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   131
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   132
 * <p>A stream should be operated on (invoking an intermediate or terminal stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   133
 * operation) only once.  This rules out, for example, "forked" streams, where
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   134
 * the same source feeds two or more pipelines, or multiple traversals of the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   135
 * same stream.  A stream implementation may throw {@link IllegalStateException}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   136
 * if it detects that the stream is being reused. However, since some stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   137
 * operations may return their receiver rather than a new stream object, it may
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   138
 * not be possible to detect reuse in all cases.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
 *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   140
 * <p>Streams have a {@link #close()} method and implement {@link AutoCloseable}.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   141
 * Operating on a stream after it has been closed will throw {@link IllegalStateException}.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   142
 * Most stream instances do not actually need to be closed after use, as they
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   143
 * are backed by collections, arrays, or generating functions, which require no
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   144
 * special resource management. Generally, only streams whose source is an IO channel,
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   145
 * such as those returned by {@link Files#lines(Path)}, will require closing. If a
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   146
 * stream does require closing, it must be opened as a resource within a try-with-resources
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   147
 * statement or similar control structure to ensure that it is closed promptly after its
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 29103
diff changeset
   148
 * operations have completed.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   149
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   150
 * <p>Stream pipelines may execute either sequentially or in
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   151
 * <a href="package-summary.html#Parallelism">parallel</a>.  This
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   152
 * execution mode is a property of the stream.  Streams are created
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   153
 * with an initial choice of sequential or parallel execution.  (For example,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   154
 * {@link Collection#stream() Collection.stream()} creates a sequential stream,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   155
 * and {@link Collection#parallelStream() Collection.parallelStream()} creates
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   156
 * a parallel one.)  This choice of execution mode may be modified by the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   157
 * {@link #sequential()} or {@link #parallel()} methods, and may be queried with
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   158
 * the {@link #isParallel()} method.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   159
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   160
 * @param <T> the type of the stream elements
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   161
 * @since 1.8
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   162
 * @see IntStream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   163
 * @see LongStream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   164
 * @see DoubleStream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
public interface Stream<T> extends BaseStream<T, Stream<T>> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   174
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   175
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   176
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   177
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   178
     *                  predicate to apply to each element to determine if it
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   179
     *                  should be included
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   180
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   181
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
    Stream<T> filter(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
     * @param <R> The element type of the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   192
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   193
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   194
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   195
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   196
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   197
    <R> Stream<R> map(Function<? super T, ? extends R> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   198
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
     * Returns an {@code IntStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   201
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   203
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   204
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   205
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   206
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   207
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   208
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   209
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   210
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
    IntStream mapToInt(ToIntFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   212
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   213
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   214
     * Returns a {@code LongStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   215
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   216
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   217
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   219
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   220
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   221
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   222
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
    LongStream mapToLong(ToLongFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   231
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   232
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   233
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   234
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   235
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   236
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   237
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
    DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   240
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   241
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   242
     * Returns a stream consisting of the results of replacing each element of
22352
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   243
     * this stream with the contents of a mapped stream produced by applying
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   244
     * the provided mapping function to each element.  Each mapped stream is
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   245
     * {@link java.util.stream.BaseStream#close() closed} after its contents
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   246
     * have been placed into this stream.  (If a mapped stream is {@code null}
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   247
     * an empty stream is used, instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   248
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   249
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   252
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   253
     * The {@code flatMap()} operation has the effect of applying a one-to-many
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   254
     * transformation to the elements of the stream, and then flattening the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   255
     * resulting elements into a new stream.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   256
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   257
     * <p><b>Examples.</b>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   258
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   259
     * <p>If {@code orders} is a stream of purchase orders, and each purchase
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   260
     * order contains a collection of line items, then the following produces a
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   261
     * stream containing all the line items in all the orders:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   262
     * <pre>{@code
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   263
     *     orders.flatMap(order -> order.getLineItems().stream())...
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   264
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   265
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   266
     * <p>If {@code path} is the path to a file, then the following produces a
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   267
     * stream of the {@code words} contained in that file:
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   268
     * <pre>{@code
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   269
     *     Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   270
     *     Stream<String> words = lines.flatMap(line -> Stream.of(line.split(" +")));
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   271
     * }</pre>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   272
     * The {@code mapper} function passed to {@code flatMap} splits a line,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   273
     * using a simple regular expression, into an array of words, and then
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   274
     * creates a stream of words from that array.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   275
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   276
     * @param <R> The element type of the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   277
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   278
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   279
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   280
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   281
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   282
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   283
    <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   284
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   285
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   286
     * Returns an {@code IntStream} consisting of the results of replacing each
22352
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   287
     * element of this stream with the contents of a mapped stream produced by
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   288
     * applying the provided mapping function to each element.  Each mapped
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   289
     * stream is {@link java.util.stream.BaseStream#close() closed} after its
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   290
     * contents have been placed into this stream.  (If a mapped stream is
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   291
     * {@code null} an empty stream is used, instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   292
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   293
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   294
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   295
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   296
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   297
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   298
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   299
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   300
     * @return the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   301
     * @see #flatMap(Function)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   302
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   303
    IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   304
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   305
    /**
22352
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   306
     * Returns an {@code LongStream} consisting of the results of replacing each
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   307
     * element of this stream with the contents of a mapped stream produced by
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   308
     * applying the provided mapping function to each element.  Each mapped
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   309
     * stream is {@link java.util.stream.BaseStream#close() closed} after its
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   310
     * contents have been placed into this stream.  (If a mapped stream is
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   311
     * {@code null} an empty stream is used, instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   312
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   313
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   314
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   315
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   316
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   317
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   318
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   319
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   320
     * @return the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   321
     * @see #flatMap(Function)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   322
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   323
    LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   324
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
    /**
22352
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   326
     * Returns an {@code DoubleStream} consisting of the results of replacing
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   327
     * each element of this stream with the contents of a mapped stream produced
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   328
     * by applying the provided mapping function to each element.  Each mapped
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   329
     * stream is {@link java.util.stream.BaseStream#close() closed} after its
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   330
     * contents have placed been into this stream.  (If a mapped stream is
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   331
     * {@code null} an empty stream is used, instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   332
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   333
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   334
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   335
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   336
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   337
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   338
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   339
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   340
     * @return the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   341
     * @see #flatMap(Function)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   342
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   343
    DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   344
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   345
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   346
     * Returns a stream consisting of the distinct elements (according to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   347
     * {@link Object#equals(Object)}) of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   348
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   349
     * <p>For ordered streams, the selection of distinct elements is stable
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   350
     * (for duplicated elements, the element appearing first in the encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   351
     * order is preserved.)  For unordered streams, no stability guarantees
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   352
     * are made.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   353
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   354
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   355
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   356
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   357
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   358
     * Preserving stability for {@code distinct()} in parallel pipelines is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   359
     * relatively expensive (requires that the operation act as a full barrier,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   360
     * with substantial buffering overhead), and stability is often not needed.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   361
     * Using an unordered stream source (such as {@link #generate(Supplier)})
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   362
     * or removing the ordering constraint with {@link #unordered()} may result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   363
     * in significantly more efficient execution for {@code distinct()} in parallel
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   364
     * pipelines, if the semantics of your situation permit.  If consistency
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   365
     * with encounter order is required, and you are experiencing poor performance
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   366
     * or memory utilization with {@code distinct()} in parallel pipelines,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   367
     * switching to sequential execution with {@link #sequential()} may improve
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   368
     * performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   369
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   370
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   371
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   372
    Stream<T> distinct();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   373
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   374
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   375
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   376
     * according to natural order.  If the elements of this stream are not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   377
     * {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   378
     * when the terminal operation is executed.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   379
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   380
     * <p>For ordered streams, the sort is stable.  For unordered streams, no
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   381
     * stability guarantees are made.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   382
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   383
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   384
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   385
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   386
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   387
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   388
    Stream<T> sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   389
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   390
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   391
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   392
     * according to the provided {@code Comparator}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   393
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   394
     * <p>For ordered streams, the sort is stable.  For unordered streams, no
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   395
     * stability guarantees are made.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   396
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   397
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   398
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   399
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   400
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   401
     *                   <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   402
     *                   {@code Comparator} to be used to compare stream elements
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   403
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   404
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   405
    Stream<T> sorted(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   406
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   407
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   408
     * Returns a stream consisting of the elements of this stream, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   409
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   410
     * from the resulting stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   411
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   414
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   419
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
     * <pre>{@code
21846
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   423
     *     Stream.of("one", "two", "three", "four")
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   424
     *         .filter(e -> e.length() > 3)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   425
     *         .peek(e -> System.out.println("Filtered value: " + e))
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   426
     *         .map(String::toUpperCase)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   427
     *         .peek(e -> System.out.println("Mapped value: " + e))
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   428
     *         .collect(Collectors.toList());
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   429
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   430
     *
38442
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   431
     * <p>In cases where stream implementation is able to optimize away the
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   432
     * production of some or all the elements (such as with short-circuiting
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   433
     * operations like {@code findFirst}, or in the example described in
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   434
     * {@link #count}), the action will not be invoked for those elements.
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   435
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   436
     * @param action a <a href="package-summary.html#NonInterference">
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   437
     *                 non-interfering</a> action to perform on the elements as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   438
     *                 they are consumed from the stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   439
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   440
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   441
    Stream<T> peek(Consumer<? super T> action);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   449
     *
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   450
     * @apiNote
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   451
     * While {@code limit()} is generally a cheap operation on sequential
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   452
     * stream pipelines, it can be quite expensive on ordered parallel pipelines,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   453
     * especially for large values of {@code maxSize}, since {@code limit(n)}
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   454
     * is constrained to return not just any <em>n</em> elements, but the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   455
     * <em>first n</em> elements in the encounter order.  Using an unordered
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   456
     * stream source (such as {@link #generate(Supplier)}) or removing the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   457
     * ordering constraint with {@link #unordered()} may result in significant
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   458
     * speedups of {@code limit()} in parallel pipelines, if the semantics of
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   459
     * your situation permit.  If consistency with encounter order is required,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   460
     * and you are experiencing poor performance or memory utilization with
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   461
     * {@code limit()} in parallel pipelines, switching to sequential execution
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   462
     * with {@link #sequential()} may improve performance.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   463
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   464
     * @param maxSize the number of elements the stream should be limited to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   465
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   466
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   467
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   468
    Stream<T> limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   469
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   470
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   471
     * Returns a stream consisting of the remaining elements of this stream
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   472
     * after discarding the first {@code n} elements of the stream.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   473
     * If this stream contains fewer than {@code n} elements then an
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   475
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     *
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   479
     * @apiNote
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   480
     * While {@code skip()} is generally a cheap operation on sequential
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   481
     * stream pipelines, it can be quite expensive on ordered parallel pipelines,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   482
     * especially for large values of {@code n}, since {@code skip(n)}
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   483
     * is constrained to skip not just any <em>n</em> elements, but the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   484
     * <em>first n</em> elements in the encounter order.  Using an unordered
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   485
     * stream source (such as {@link #generate(Supplier)}) or removing the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   486
     * ordering constraint with {@link #unordered()} may result in significant
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   487
     * speedups of {@code skip()} in parallel pipelines, if the semantics of
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   488
     * your situation permit.  If consistency with encounter order is required,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   489
     * and you are experiencing poor performance or memory utilization with
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   490
     * {@code skip()} in parallel pipelines, switching to sequential execution
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   491
     * with {@link #sequential()} may improve performance.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   492
     *
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   493
     * @param n the number of leading elements to skip
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
     * @return the new stream
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   495
     * @throws IllegalArgumentException if {@code n} is negative
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   496
     */
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   497
    Stream<T> skip(long n);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   498
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   499
    /**
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   500
     * Returns, if this stream is ordered, a stream consisting of the longest
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   501
     * prefix of elements taken from this stream that match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   502
     * Otherwise returns, if this stream is unordered, a stream consisting of a
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   503
     * subset of elements taken from this stream that match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   504
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   505
     * <p>If this stream is ordered then the longest prefix is a contiguous
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   506
     * sequence of elements of this stream that match the given predicate.  The
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   507
     * first element of the sequence is the first element of this stream, and
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   508
     * the element immediately following the last element of the sequence does
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   509
     * not match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   510
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   511
     * <p>If this stream is unordered, and some (but not all) elements of this
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   512
     * stream match the given predicate, then the behavior of this operation is
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   513
     * nondeterministic; it is free to take any subset of matching elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   514
     * (which includes the empty set).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   515
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   516
     * <p>Independent of whether this stream is ordered or unordered if all
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   517
     * elements of this stream match the given predicate then this operation
32002
e378c0dc767e 8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents: 31644
diff changeset
   518
     * takes all elements (the result is the same as the input), or if no
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   519
     * elements of the stream match the given predicate then no elements are
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   520
     * taken (the result is an empty stream).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   521
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   522
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   523
     * stateful intermediate operation</a>.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   524
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   525
     * @implSpec
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   526
     * The default implementation obtains the {@link #spliterator() spliterator}
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   527
     * of this stream, wraps that spliterator so as to support the semantics
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   528
     * of this operation on traversal, and returns a new stream associated with
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   529
     * the wrapped spliterator.  The returned stream preserves the execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   530
     * characteristics of this stream (namely parallel or sequential execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   531
     * as per {@link #isParallel()}) but the wrapped spliterator may choose to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   532
     * not support splitting.  When the returned stream is closed, the close
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   533
     * handlers for both the returned and this stream are invoked.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   534
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   535
     * @apiNote
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   536
     * While {@code takeWhile()} is generally a cheap operation on sequential
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   537
     * stream pipelines, it can be quite expensive on ordered parallel
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   538
     * pipelines, since the operation is constrained to return not just any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   539
     * valid prefix, but the longest prefix of elements in the encounter order.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   540
     * Using an unordered stream source (such as {@link #generate(Supplier)}) or
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   541
     * removing the ordering constraint with {@link #unordered()} may result in
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   542
     * significant speedups of {@code takeWhile()} in parallel pipelines, if the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   543
     * semantics of your situation permit.  If consistency with encounter order
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   544
     * is required, and you are experiencing poor performance or memory
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   545
     * utilization with {@code takeWhile()} in parallel pipelines, switching to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   546
     * sequential execution with {@link #sequential()} may improve performance.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   547
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   548
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   549
     *                  <a href="package-summary.html#Statelessness">stateless</a>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   550
     *                  predicate to apply to elements to determine the longest
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   551
     *                  prefix of elements.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   552
     * @return the new stream
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32017
diff changeset
   553
     * @since 9
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   554
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   555
    default Stream<T> takeWhile(Predicate<? super T> predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   556
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   557
        // Reuses the unordered spliterator, which, when encounter is present,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   558
        // is safe to use as long as it configured not to split
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   559
        return StreamSupport.stream(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   560
                new WhileOps.UnorderedWhileSpliterator.OfRef.Taking<>(spliterator(), true, predicate),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   561
                isParallel()).onClose(this::close);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   562
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   563
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   564
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   565
     * Returns, if this stream is ordered, a stream consisting of the remaining
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   566
     * elements of this stream after dropping the longest prefix of elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   567
     * that match the given predicate.  Otherwise returns, if this stream is
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   568
     * unordered, a stream consisting of the remaining elements of this stream
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   569
     * after dropping a subset of elements that match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   570
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   571
     * <p>If this stream is ordered then the longest prefix is a contiguous
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   572
     * sequence of elements of this stream that match the given predicate.  The
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   573
     * first element of the sequence is the first element of this stream, and
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   574
     * the element immediately following the last element of the sequence does
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   575
     * not match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   576
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   577
     * <p>If this stream is unordered, and some (but not all) elements of this
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   578
     * stream match the given predicate, then the behavior of this operation is
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   579
     * nondeterministic; it is free to drop any subset of matching elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   580
     * (which includes the empty set).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   581
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   582
     * <p>Independent of whether this stream is ordered or unordered if all
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   583
     * elements of this stream match the given predicate then this operation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   584
     * drops all elements (the result is an empty stream), or if no elements of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   585
     * the stream match the given predicate then no elements are dropped (the
32002
e378c0dc767e 8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents: 31644
diff changeset
   586
     * result is the same as the input).
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   587
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   588
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   589
     * intermediate operation</a>.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   590
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   591
     * @implSpec
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   592
     * The default implementation obtains the {@link #spliterator() spliterator}
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   593
     * of this stream, wraps that spliterator so as to support the semantics
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   594
     * of this operation on traversal, and returns a new stream associated with
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   595
     * the wrapped spliterator.  The returned stream preserves the execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   596
     * characteristics of this stream (namely parallel or sequential execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   597
     * as per {@link #isParallel()}) but the wrapped spliterator may choose to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   598
     * not support splitting.  When the returned stream is closed, the close
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   599
     * handlers for both the returned and this stream are invoked.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   600
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   601
     * @apiNote
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   602
     * While {@code dropWhile()} is generally a cheap operation on sequential
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   603
     * stream pipelines, it can be quite expensive on ordered parallel
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   604
     * pipelines, since the operation is constrained to return not just any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   605
     * valid prefix, but the longest prefix of elements in the encounter order.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   606
     * Using an unordered stream source (such as {@link #generate(Supplier)}) or
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   607
     * removing the ordering constraint with {@link #unordered()} may result in
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   608
     * significant speedups of {@code dropWhile()} in parallel pipelines, if the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   609
     * semantics of your situation permit.  If consistency with encounter order
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   610
     * is required, and you are experiencing poor performance or memory
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   611
     * utilization with {@code dropWhile()} in parallel pipelines, switching to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   612
     * sequential execution with {@link #sequential()} may improve performance.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   613
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   614
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   615
     *                  <a href="package-summary.html#Statelessness">stateless</a>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   616
     *                  predicate to apply to elements to determine the longest
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   617
     *                  prefix of elements.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   618
     * @return the new stream
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32017
diff changeset
   619
     * @since 9
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   620
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   621
    default Stream<T> dropWhile(Predicate<? super T> predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   622
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   623
        // Reuses the unordered spliterator, which, when encounter is present,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   624
        // is safe to use as long as it configured not to split
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   625
        return StreamSupport.stream(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   626
                new WhileOps.UnorderedWhileSpliterator.OfRef.Dropping<>(spliterator(), true, predicate),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   627
                isParallel()).onClose(this::close);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   628
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   629
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   630
    /**
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   633
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   634
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   635
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   636
     * <p>The behavior of this operation is explicitly nondeterministic.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   637
     * For parallel stream pipelines, this operation does <em>not</em>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   638
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   639
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   640
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   641
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   643
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   644
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   645
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   646
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   647
    void forEach(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   648
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   649
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   650
     * Performs an action for each element of this stream, in the encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   651
     * order of the stream if the stream has a defined encounter order.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   652
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   653
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   654
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   655
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   656
     * <p>This operation processes the elements one at a time, in encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   657
     * order if one exists.  Performing the action for one element
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   658
     * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   659
     * performing the action for subsequent elements, but for any given element,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   660
     * the action may be performed in whatever thread the library chooses.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   661
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   662
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   663
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   664
     * @see #forEach(Consumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   665
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   666
    void forEachOrdered(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   667
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   668
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   669
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   670
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   671
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   672
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   673
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   674
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   675
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   676
    Object[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   677
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   678
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   679
     * Returns an array containing the elements of this stream, using the
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   680
     * provided {@code generator} function to allocate the returned array, as
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   681
     * well as any additional arrays that might be required for a partitioned
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   682
     * execution or for resizing.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   683
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   684
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   685
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   686
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   687
     * @apiNote
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   688
     * The generator function takes an integer, which is the size of the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   689
     * desired array, and produces an array of the desired size.  This can be
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   690
     * concisely expressed with an array constructor reference:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   691
     * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   692
     *     Person[] men = people.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   693
     *                          .filter(p -> p.getGender() == MALE)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   694
     *                          .toArray(Person[]::new);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   695
     * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   696
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   697
     * @param <A> the element type of the resulting array
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   698
     * @param generator a function which produces a new array of the desired
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   699
     *                  type and the provided length
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   700
     * @return an array containing the elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   701
     * @throws ArrayStoreException if the runtime type of the array returned
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   702
     * from the array generator is not a supertype of the runtime type of every
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   703
     * element in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   704
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   705
    <A> A[] toArray(IntFunction<A[]> generator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   706
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   707
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   708
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   709
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   710
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   711
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   712
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   713
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   714
     *     T result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   715
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   716
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   717
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   718
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   719
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   720
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   721
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   722
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   723
     * function. This means that for all {@code t},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   724
     * {@code accumulator.apply(identity, t)} is equal to {@code t}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   725
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   726
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   727
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   728
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   729
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   730
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   731
     * @apiNote Sum, min, max, average, and string concatenation are all special
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   732
     * cases of reduction. Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   733
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   734
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   735
     *     Integer sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   736
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   737
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   738
     * or:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   739
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   740
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   741
     *     Integer sum = integers.reduce(0, Integer::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   742
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   743
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   744
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   745
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   746
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   747
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   748
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   749
     * @param identity the identity value for the accumulating function
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   750
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   751
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   752
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   753
     *                    function for combining two values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   754
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   755
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   756
    T reduce(T identity, BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   757
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   758
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   759
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   760
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   761
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   762
     * function, and returns an {@code Optional} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   763
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   764
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   765
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   766
     *     T result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   767
     *     for (T element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   768
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   769
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   770
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   771
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   772
     *         else
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   773
     *             result = accumulator.apply(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   774
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   775
     *     return foundAny ? Optional.of(result) : Optional.empty();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   776
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   777
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   778
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   779
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   780
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   781
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   782
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   783
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   784
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   785
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   786
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   787
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   788
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   789
     *                    function for combining two values
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   790
     * @return an {@link Optional} describing the result of the reduction
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   791
     * @throws NullPointerException if the result of the reduction is null
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   792
     * @see #reduce(Object, BinaryOperator)
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   793
     * @see #min(Comparator)
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   794
     * @see #max(Comparator)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   795
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   796
    Optional<T> reduce(BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   797
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   798
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   799
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
19859
ac48498acd3a 8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents: 19850
diff changeset
   800
     * elements of this stream, using the provided identity, accumulation and
ac48498acd3a 8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents: 19850
diff changeset
   801
     * combining functions.  This is equivalent to:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   802
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   803
     *     U result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   804
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   805
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   806
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   807
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   808
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   809
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   810
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   811
     * <p>The {@code identity} value must be an identity for the combiner
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   812
     * function.  This means that for all {@code u}, {@code combiner(identity, u)}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   813
     * is equal to {@code u}.  Additionally, the {@code combiner} function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   814
     * must be compatible with the {@code accumulator} function; for all
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   815
     * {@code u} and {@code t}, the following must hold:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   816
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   817
     *     combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   818
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   819
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   820
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   821
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   822
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   823
     * @apiNote Many reductions using this form can be represented more simply
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   824
     * by an explicit combination of {@code map} and {@code reduce} operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   825
     * The {@code accumulator} function acts as a fused mapper and accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   826
     * which can sometimes be more efficient than separate mapping and reduction,
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   827
     * such as when knowing the previously reduced value allows you to avoid
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   828
     * some computation.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   829
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   830
     * @param <U> The type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   831
     * @param identity the identity value for the combiner function
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   832
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   833
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   834
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   835
     *                    function for incorporating an additional element into a result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   836
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   837
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   838
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   839
     *                    function for combining two values, which must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   840
     *                    compatible with the accumulator function
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   841
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   842
     * @see #reduce(BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   843
     * @see #reduce(Object, BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   844
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   845
    <U> U reduce(U identity,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   846
                 BiFunction<U, ? super T, U> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   847
                 BinaryOperator<U> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   848
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   849
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   850
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   851
     * reduction</a> operation on the elements of this stream.  A mutable
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   852
     * reduction is one in which the reduced value is a mutable result container,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   853
     * such as an {@code ArrayList}, and elements are incorporated by updating
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   854
     * the state of the result rather than by replacing the result.  This
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   855
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   856
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   857
     *     R result = supplier.get();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   858
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   859
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   860
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   861
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   862
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   863
     * <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   864
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   865
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   866
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   867
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   868
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   869
     * @apiNote There are many existing classes in the JDK whose signatures are
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   870
     * well-suited for use with method references as arguments to {@code collect()}.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   871
     * For example, the following will accumulate strings into an {@code ArrayList}:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   872
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   873
     *     List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   874
     *                                                ArrayList::addAll);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   875
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   876
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   877
     * <p>The following will take a stream of strings and concatenates them into a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   878
     * single string:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   879
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   880
     *     String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   881
     *                                          StringBuilder::append)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   882
     *                                 .toString();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   883
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   884
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   885
     * @param <R> type of the result
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   886
     * @param supplier a function that creates a new result container. For a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   887
     *                 parallel execution, this function may be called
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   888
     *                 multiple times and must return a fresh value each time.
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   889
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   890
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   891
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   892
     *                    function for incorporating an additional element into a result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   893
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   894
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   895
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   896
     *                    function for combining two values, which must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   897
     *                    compatible with the accumulator function
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   898
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   899
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   900
    <R> R collect(Supplier<R> supplier,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   901
                  BiConsumer<R, ? super T> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   902
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   903
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   904
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   905
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   906
     * reduction</a> operation on the elements of this stream using a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   907
     * {@code Collector}.  A {@code Collector}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   908
     * encapsulates the functions used as arguments to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   909
     * {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   910
     * collection strategies and composition of collect operations such as
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   911
     * multiple-level grouping or partitioning.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   912
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   913
     * <p>If the stream is parallel, and the {@code Collector}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   914
     * is {@link Collector.Characteristics#CONCURRENT concurrent}, and
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   915
     * either the stream is unordered or the collector is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   916
     * {@link Collector.Characteristics#UNORDERED unordered},
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   917
     * then a concurrent reduction will be performed (see {@link Collector} for
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   918
     * details on concurrent reduction.)
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   919
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   920
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   921
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   922
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   923
     * <p>When executed in parallel, multiple intermediate results may be
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   924
     * instantiated, populated, and merged so as to maintain isolation of
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   925
     * mutable data structures.  Therefore, even when executed in parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   926
     * with non-thread-safe data structures (such as {@code ArrayList}), no
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   927
     * additional synchronization is needed for a parallel reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   928
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   929
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   930
     * The following will accumulate strings into an ArrayList:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   931
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   932
     *     List<String> asList = stringStream.collect(Collectors.toList());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   933
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   934
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   935
     * <p>The following will classify {@code Person} objects by city:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   936
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   937
     *     Map<String, List<Person>> peopleByCity
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   938
     *         = personStream.collect(Collectors.groupingBy(Person::getCity));
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   939
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   940
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   941
     * <p>The following will classify {@code Person} objects by state and city,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   942
     * cascading two {@code Collector}s together:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   943
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   944
     *     Map<String, Map<String, List<Person>>> peopleByStateAndCity
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   945
     *         = personStream.collect(Collectors.groupingBy(Person::getState,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   946
     *                                                      Collectors.groupingBy(Person::getCity)));
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   947
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   948
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   949
     * @param <R> the type of the result
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19199
diff changeset
   950
     * @param <A> the intermediate accumulation type of the {@code Collector}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   951
     * @param collector the {@code Collector} describing the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   952
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   953
     * @see #collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   954
     * @see Collectors
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   955
     */
19799
efa9ff09b024 8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos
henryjen
parents: 19214
diff changeset
   956
    <R, A> R collect(Collector<? super T, A, R> collector);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   957
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   958
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   959
     * Returns the minimum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   960
     * {@code Comparator}.  This is a special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   961
     * <a href="package-summary.html#Reduction">reduction</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   962
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   963
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   964
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   965
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   966
     *                   <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   967
     *                   {@code Comparator} to compare elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   968
     * @return an {@code Optional} describing the minimum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   969
     * or an empty {@code Optional} if the stream is empty
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   970
     * @throws NullPointerException if the minimum element is null
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   971
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   972
    Optional<T> min(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   973
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   974
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   975
     * Returns the maximum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   976
     * {@code Comparator}.  This is a special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   977
     * <a href="package-summary.html#Reduction">reduction</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   978
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   979
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   980
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   981
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   982
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   983
     *                   <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   984
     *                   {@code Comparator} to compare elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   985
     * @return an {@code Optional} describing the maximum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   986
     * or an empty {@code Optional} if the stream is empty
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   987
     * @throws NullPointerException if the maximum element is null
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   988
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   989
    Optional<T> max(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   990
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   991
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   992
     * Returns the count of elements in this stream.  This is a special case of
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   993
     * a <a href="package-summary.html#Reduction">reduction</a> and is
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   994
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   995
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   996
     *     return mapToLong(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   997
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   998
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   999
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1000
     *
29489
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1001
     * @apiNote
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1002
     * An implementation may choose to not execute the stream pipeline (either
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1003
     * sequentially or in parallel) if it is capable of computing the count
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1004
     * directly from the stream source.  In such cases no source elements will
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1005
     * be traversed and no intermediate operations will be evaluated.
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1006
     * Behavioral parameters with side-effects, which are strongly discouraged
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1007
     * except for harmless cases such as debugging, may be affected.  For
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1008
     * example, consider the following stream:
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1009
     * <pre>{@code
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1010
     *     List<String> l = Arrays.asList("A", "B", "C", "D");
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1011
     *     long count = l.stream().peek(System.out::println).count();
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1012
     * }</pre>
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1013
     * The number of elements covered by the stream source, a {@code List}, is
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1014
     * known and the intermediate operation, {@code peek}, does not inject into
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1015
     * or remove elements from the stream (as may be the case for
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1016
     * {@code flatMap} or {@code filter} operations).  Thus the count is the
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1017
     * size of the {@code List} and there is no need to execute the pipeline
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1018
     * and, as a side-effect, print out the list elements.
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29269
diff changeset
  1019
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1020
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1021
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1022
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1023
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1024
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1025
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1026
     * predicate.  May not evaluate the predicate on all elements if not
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1027
     * necessary for determining the result.  If the stream is empty then
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1028
     * {@code false} is returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1029
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1030
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1031
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1032
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1033
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1034
     * This method evaluates the <em>existential quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1035
     * predicate over the elements of the stream (for some x P(x)).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1036
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1037
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1038
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1039
     *                  predicate to apply to elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1040
     * @return {@code true} if any elements of the stream match the provided
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1041
     * predicate, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1042
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1043
    boolean anyMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1044
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1045
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1046
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1047
     * May not evaluate the predicate on all elements if not necessary for
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1048
     * determining the result.  If the stream is empty then {@code true} is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1049
     * returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1050
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1051
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1052
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1053
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1054
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1055
     * This method evaluates the <em>universal quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1056
     * predicate over the elements of the stream (for all x P(x)).  If the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1057
     * stream is empty, the quantification is said to be <em>vacuously
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1058
     * satisfied</em> and is always {@code true} (regardless of P(x)).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1059
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1060
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1061
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1062
     *                  predicate to apply to elements of this stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1063
     * @return {@code true} if either all elements of the stream match the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1064
     * provided predicate or the stream is empty, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1065
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1066
    boolean allMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1067
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1068
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1069
     * Returns whether no elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1070
     * May not evaluate the predicate on all elements if not necessary for
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1071
     * determining the result.  If the stream is empty then {@code true} is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1072
     * returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1073
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1074
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1075
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1076
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1077
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1078
     * This method evaluates the <em>universal quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1079
     * negated predicate over the elements of the stream (for all x ~P(x)).  If
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1080
     * the stream is empty, the quantification is said to be vacuously satisfied
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1081
     * and is always {@code true}, regardless of P(x).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1082
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1083
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1084
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1085
     *                  predicate to apply to elements of this stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1086
     * @return {@code true} if either no elements of the stream match the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1087
     * provided predicate or the stream is empty, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1088
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1089
    boolean noneMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1090
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1091
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1092
     * Returns an {@link Optional} describing the first element of this stream,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1093
     * or an empty {@code Optional} if the stream is empty.  If the stream has
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1094
     * no encounter order, then any element may be returned.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1095
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1096
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1097
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1098
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1099
     * @return an {@code Optional} describing the first element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1100
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1101
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1102
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1103
    Optional<T> findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1104
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1105
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1106
     * Returns an {@link Optional} describing some element of the stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1107
     * empty {@code Optional} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1108
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1109
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1110
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1111
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1112
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1113
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1114
     * performance in parallel operations; the cost is that multiple invocations
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1115
     * on the same source may not return the same result.  (If a stable result
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1116
     * is desired, use {@link #findFirst()} instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1117
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1118
     * @return an {@code Optional} describing some element of this stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1119
     * empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1120
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1121
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1122
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1123
    Optional<T> findAny();
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1124
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1125
    // Static factories
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1126
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1127
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1128
     * Returns a builder for a {@code Stream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1129
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1130
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1131
     * @return a stream builder
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1132
     */
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1133
    public static<T> Builder<T> builder() {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1134
        return new Streams.StreamBuilderImpl<>();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1135
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1136
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1137
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1138
     * Returns an empty sequential {@code Stream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1139
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1140
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1141
     * @return an empty sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1142
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1143
    public static<T> Stream<T> empty() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1144
        return StreamSupport.stream(Spliterators.<T>emptySpliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1145
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1146
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1147
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1148
     * Returns a sequential {@code Stream} containing a single element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1149
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1150
     * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1151
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1152
     * @return a singleton sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1153
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1154
    public static<T> Stream<T> of(T t) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1155
        return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1156
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1157
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1158
    /**
28855
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1159
     * Returns a sequential {@code Stream} containing a single element, if
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1160
     * non-null, otherwise returns an empty {@code Stream}.
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1161
     *
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1162
     * @param t the single element
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1163
     * @param <T> the type of stream elements
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1164
     * @return a stream with a single element if the specified element
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1165
     *         is non-null, otherwise an empty stream
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32017
diff changeset
  1166
     * @since 9
28855
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1167
     */
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1168
    public static<T> Stream<T> ofNullable(T t) {
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1169
        return t == null ? Stream.empty()
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1170
                         : StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1171
    }
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1172
fd65ec779433 8050819: Please add java.util.Stream.ofNullable(T object)
psandoz
parents: 25859
diff changeset
  1173
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1174
     * Returns a sequential ordered stream whose elements are the specified values.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1175
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1176
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1177
     * @param values the elements of the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1178
     * @return the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1179
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1180
    @SafeVarargs
19859
ac48498acd3a 8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents: 19850
diff changeset
  1181
    @SuppressWarnings("varargs") // Creating a stream from an array is safe
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1182
    public static<T> Stream<T> of(T... values) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1183
        return Arrays.stream(values);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1184
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1185
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1186
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1187
     * Returns an infinite sequential ordered {@code Stream} produced by iterative
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1188
     * application of a function {@code f} to an initial element {@code seed},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1189
     * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1190
     * {@code f(f(seed))}, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1191
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1192
     * <p>The first element (position {@code 0}) in the {@code Stream} will be
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1193
     * the provided {@code seed}.  For {@code n > 0}, the element at position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1194
     * {@code n}, will be the result of applying the function {@code f} to the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1195
     * element at position {@code n - 1}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1196
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1197
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1198
     * @param seed the initial element
25526
d3cbdae6e9f9 8044047: Missing null pointer checks for streams
psandoz
parents: 22352
diff changeset
  1199
     * @param f a function to be applied to the previous element to produce
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1200
     *          a new element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1201
     * @return a new sequential {@code Stream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1202
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1203
    public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1204
        Objects.requireNonNull(f);
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1205
        Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1206
               Spliterator.ORDERED | Spliterator.IMMUTABLE) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1207
            T prev;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1208
            boolean started;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1209
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1210
            @Override
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1211
            public boolean tryAdvance(Consumer<? super T> action) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1212
                Objects.requireNonNull(action);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1213
                T t;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1214
                if (started)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1215
                    t = f.apply(prev);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1216
                else {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1217
                    t = seed;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1218
                    started = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1219
                }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1220
                action.accept(prev = t);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1221
                return true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1222
            }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1223
        };
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1224
        return StreamSupport.stream(spliterator, false);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1225
    }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1226
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1227
    /**
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1228
     * Returns a sequential ordered {@code Stream} produced by iterative
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1229
     * application of a function to an initial element, conditioned on
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1230
     * satisfying the supplied predicate.  The stream terminates as soon as
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1231
     * the predicate returns false.
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1232
     *
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1233
     * <p>
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1234
     * {@code Stream.iterate} should produce the same sequence of elements as
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1235
     * produced by the corresponding for-loop:
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1236
     * <pre>{@code
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1237
     *     for (T index=seed; predicate.test(index); index = f.apply(index)) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1238
     *         ...
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1239
     *     }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1240
     * }</pre>
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1241
     *
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1242
     * <p>
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1243
     * The resulting sequence may be empty if the predicate does not hold on
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1244
     * the seed value.  Otherwise the first element will be the supplied seed
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1245
     * value, the next element (if present) will be the result of applying the
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1246
     * function f to the seed value, and so on iteratively until the predicate
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1247
     * indicates that the stream should terminate.
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1248
     *
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1249
     * @param <T> the type of stream elements
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1250
     * @param seed the initial element
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1251
     * @param predicate a predicate to apply to elements to determine when the
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1252
     *          stream must terminate.
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1253
     * @param f a function to be applied to the previous element to produce
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1254
     *          a new element
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1255
     * @return a new sequential {@code Stream}
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1256
     * @since 9
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1257
     */
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1258
    public static<T> Stream<T> iterate(T seed, Predicate<? super T> predicate, UnaryOperator<T> f) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1259
        Objects.requireNonNull(f);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1260
        Objects.requireNonNull(predicate);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1261
        Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1262
               Spliterator.ORDERED | Spliterator.IMMUTABLE) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1263
            T prev;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1264
            boolean started, finished;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1265
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1266
            @Override
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1267
            public boolean tryAdvance(Consumer<? super T> action) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1268
                Objects.requireNonNull(action);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1269
                if (finished)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1270
                    return false;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1271
                T t;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1272
                if (started)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1273
                    t = f.apply(prev);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1274
                else {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1275
                    t = seed;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1276
                    started = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1277
                }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1278
                if (!predicate.test(t)) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1279
                    prev = null;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1280
                    finished = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1281
                    return false;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1282
                }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1283
                action.accept(prev = t);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1284
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1285
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1286
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1287
            @Override
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1288
            public void forEachRemaining(Consumer<? super T> action) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1289
                Objects.requireNonNull(action);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1290
                if (finished)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1291
                    return;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1292
                finished = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1293
                T t = started ? f.apply(prev) : seed;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1294
                prev = null;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1295
                while (predicate.test(t)) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1296
                    action.accept(t);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1297
                    t = f.apply(t);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1298
                }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1299
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1300
        };
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
  1301
        return StreamSupport.stream(spliterator, false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1302
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1303
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1304
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1305
     * Returns an infinite sequential unordered stream where each element is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1306
     * generated by the provided {@code Supplier}.  This is suitable for
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1307
     * generating constant streams, streams of random elements, etc.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1308
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1309
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1310
     * @param s the {@code Supplier} of generated elements
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1311
     * @return a new infinite sequential unordered {@code Stream}
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1312
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1313
    public static<T> Stream<T> generate(Supplier<T> s) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1314
        Objects.requireNonNull(s);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17914
diff changeset
  1315
        return StreamSupport.stream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1316
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1317
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1318
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1319
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1320
     * Creates a lazily concatenated stream whose elements are all the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1321
     * elements of the first stream followed by all the elements of the
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1322
     * second stream.  The resulting stream is ordered if both
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1323
     * of the input streams are ordered, and parallel if either of the input
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1324
     * streams is parallel.  When the resulting stream is closed, the close
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1325
     * handlers for both input streams are invoked.
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1326
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1327
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1328
     * Use caution when constructing streams from repeated concatenation.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1329
     * Accessing an element of a deeply concatenated stream can result in deep
29103
1dbde34ad64c 8073779: StackOverflowError called StackOverflowException in javadoc
igerasim
parents: 28855
diff changeset
  1330
     * call chains, or even {@code StackOverflowError}.
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1331
     *
32017
f58d32041dd8 8132800: clarify stream package documentation regarding sequential vs parallel modes
smarks
parents: 32002
diff changeset
  1332
     * <p>Subsequent changes to the sequential/parallel execution mode of the
f58d32041dd8 8132800: clarify stream package documentation regarding sequential vs parallel modes
smarks
parents: 32002
diff changeset
  1333
     * returned stream are not guaranteed to be propagated to the input streams.
f58d32041dd8 8132800: clarify stream package documentation regarding sequential vs parallel modes
smarks
parents: 32002
diff changeset
  1334
     *
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1335
     * @param <T> The type of stream elements
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1336
     * @param a the first stream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1337
     * @param b the second stream
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1338
     * @return the concatenation of the two input streams
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1339
     */
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1340
    public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1341
        Objects.requireNonNull(a);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1342
        Objects.requireNonNull(b);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1343
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1344
        @SuppressWarnings("unchecked")
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1345
        Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1346
                (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1347
        Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1348
        return stream.onClose(Streams.composedClose(a, b));
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1349
    }
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1350
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1351
    /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1352
     * A mutable builder for a {@code Stream}.  This allows the creation of a
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1353
     * {@code Stream} by generating elements individually and adding them to the
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1354
     * {@code Builder} (without the copying overhead that comes from using
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1355
     * an {@code ArrayList} as a temporary buffer.)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1356
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1357
     * <p>A stream builder has a lifecycle, which starts in a building
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1358
     * phase, during which elements can be added, and then transitions to a built
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1359
     * phase, after which elements may not be added.  The built phase begins
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1360
     * when the {@link #build()} method is called, which creates an ordered
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1361
     * {@code Stream} whose elements are the elements that were added to the stream
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1362
     * builder, in the order they were added.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1363
     *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1364
     * @param <T> the type of stream elements
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1365
     * @see Stream#builder()
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1366
     * @since 1.8
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1367
     */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1368
    public interface Builder<T> extends Consumer<T> {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1369
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1370
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1371
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1372
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1373
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1374
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1375
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1376
        @Override
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1377
        void accept(T t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1378
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1379
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1380
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1381
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1382
         * @implSpec
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1383
         * The default implementation behaves as if:
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1384
         * <pre>{@code
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1385
         *     accept(t)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1386
         *     return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1387
         * }</pre>
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1388
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1389
         * @param t the element to add
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1390
         * @return {@code this} builder
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1391
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1392
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1393
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1394
        default Builder<T> add(T t) {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1395
            accept(t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1396
            return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1397
        }
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1398
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1399
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1400
         * Builds the stream, transitioning this builder to the built state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1401
         * An {@code IllegalStateException} is thrown if there are further attempts
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1402
         * to operate on the builder after it has entered the built state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1403
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1404
         * @return the built stream
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1405
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1406
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1407
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1408
        Stream<T> build();
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1409
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1410
    }
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1411
}