jdk/src/share/classes/java/util/stream/Stream.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 21846 c10feb34bc0b
child 22352 ecbf37860ffa
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     1
/*
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
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.charset.Charset;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    28
import java.nio.file.Files;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    29
import java.nio.file.Path;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    30
import java.util.Arrays;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    31
import java.util.Collection;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.Comparator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    33
import java.util.Iterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    34
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
import java.util.Optional;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    36
import java.util.Spliterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    37
import java.util.Spliterators;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    38
import java.util.concurrent.ConcurrentHashMap;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    39
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
import java.util.function.BiFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    41
import java.util.function.BinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
import java.util.function.Consumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
import java.util.function.IntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    45
import java.util.function.Predicate;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    46
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    47
import java.util.function.ToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    48
import java.util.function.ToIntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    49
import java.util.function.ToLongFunction;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    50
import java.util.function.UnaryOperator;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    51
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    52
/**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    53
 * A sequence of elements supporting sequential and parallel aggregate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    54
 * operations.  The following example illustrates an aggregate operation using
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    55
 * {@link Stream} and {@link IntStream}:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    56
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    57
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    58
 *     int sum = widgets.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    59
 *                      .filter(w -> w.getColor() == RED)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    60
 *                      .mapToInt(w -> w.getWeight())
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    61
 *                      .sum();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    62
 * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    63
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    64
 * 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
    65
 * 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
    66
 * 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
    67
 * 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
    68
 * each red widget. Then this stream is summed to produce a total weight.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    69
 *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    70
 * <p>In addition to {@code Stream}, which is a stream of object references,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    71
 * there are primitive specializations for {@link IntStream}, {@link LongStream},
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    72
 * and {@link DoubleStream}, all of which are referred to as "streams" and
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    73
 * conform to the characteristics and restrictions described here.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    74
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    75
 * <p>To perform a computation, stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    76
 * <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
    77
 * <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
    78
 * 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
    79
 * etc), zero or more <em>intermediate operations</em> (which transform a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    80
 * 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
    81
 * <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
    82
 * as {@link Stream#count()} or {@link Stream#forEach(Consumer)}).
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    83
 * 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
    84
 * terminal operation is initiated, and source elements are consumed only
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    85
 * as needed.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    86
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    87
 * <p>Collections and streams, while bearing some superficial similarities,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    88
 * have different goals.  Collections are primarily concerned with the efficient
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    89
 * 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
    90
 * 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
    91
 * instead concerned with declaratively describing their source and the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    92
 * computational operations which will be performed in aggregate on that source.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    93
 * However, if the provided stream operations do not offer the desired
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    94
 * functionality, the {@link #iterator()} and {@link #spliterator()} operations
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    95
 * can be used to perform a controlled traversal.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    96
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    97
 * <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
    98
 * 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
    99
 * designed for concurrent modification (such as a {@link ConcurrentHashMap}),
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   100
 * unpredictable or erroneous behavior may result from modifying the stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   101
 * source while it is being queried.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   102
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   103
 * <p>Most stream operations accept parameters that describe user-specified
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   104
 * behavior, such as the lambda expression {@code w -> w.getWeight()} passed to
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   105
 * {@code mapToInt} in the example above.  To preserve correct behavior,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   106
 * these <em>behavioral parameters</em>:
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   107
 * <ul>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   108
 * <li>must be <a href="package-summary.html#NonInterference">non-interfering</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   109
 * (they do not modify the stream source); and</li>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   110
 * <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
   111
 * (their result should not depend on any state that might change during execution
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   112
 * of the stream pipeline).</li>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   113
 * </ul>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   114
 *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   115
 * <p>Such parameters are always instances of a
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   116
 * <a href="../function/package-summary.html">functional interface</a> such
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   117
 * as {@link java.util.function.Function}, and are often lambda expressions or
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   118
 * method references.  Unless otherwise specified these parameters must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   119
 * <em>non-null</em>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   120
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   121
 * <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
   122
 * operation) only once.  This rules out, for example, "forked" streams, where
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   123
 * 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
   124
 * same stream.  A stream implementation may throw {@link IllegalStateException}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   125
 * 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
   126
 * 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
   127
 * not be possible to detect reuse in all cases.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   128
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   129
 * <p>Streams have a {@link #close()} method and implement {@link AutoCloseable},
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   130
 * but nearly all stream instances do not actually need to be closed after use.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   131
 * Generally, only streams whose source is an IO channel (such as those returned
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   132
 * by {@link Files#lines(Path, Charset)}) will require closing.  Most streams
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   133
 * are backed by collections, arrays, or generating functions, which require no
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   134
 * special resource management.  (If a stream does require closing, it can be
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   135
 * declared as a resource in a {@code try}-with-resources statement.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   137
 * <p>Stream pipelines may execute either sequentially or in
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   138
 * <a href="package-summary.html#Parallelism">parallel</a>.  This
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   139
 * execution mode is a property of the stream.  Streams are created
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   140
 * with an initial choice of sequential or parallel execution.  (For example,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   141
 * {@link Collection#stream() Collection.stream()} creates a sequential stream,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   142
 * and {@link Collection#parallelStream() Collection.parallelStream()} creates
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   143
 * 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
   144
 * {@link #sequential()} or {@link #parallel()} methods, and may be queried with
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   145
 * the {@link #isParallel()} method.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   146
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   147
 * @param <T> the type of the stream elements
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   148
 * @since 1.8
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   149
 * @see IntStream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   150
 * @see LongStream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   151
 * @see DoubleStream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   152
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   153
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   154
public interface Stream<T> extends BaseStream<T, Stream<T>> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   155
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   156
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   158
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   159
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   161
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   163
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   164
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   165
     *                  predicate to apply to each element to determine if it
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   166
     *                  should be included
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
    Stream<T> filter(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   174
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   175
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   176
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   177
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   178
     * @param <R> The element type of the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   179
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   180
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   181
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
    <R> Stream<R> map(Function<? super T, ? extends R> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
     * Returns an {@code IntStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   193
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   194
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   195
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   196
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   197
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   198
    IntStream mapToInt(ToIntFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   201
     * Returns a {@code LongStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   203
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   204
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   205
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   206
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   207
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   208
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   209
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   210
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   212
    LongStream mapToLong(ToLongFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   213
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   214
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   215
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   216
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   217
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   219
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   221
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   222
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   223
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
    DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
     * Returns a stream consisting of the results of replacing each element of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
     * this stream with the contents of the stream produced by applying the
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   231
     * provided mapping function to each element.  (If the result of the mapping
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   232
     * function is {@code null}, this is treated as if the result was an empty
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   233
     * stream.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   234
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   235
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   236
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   237
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
     * 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
   240
     * transformation to the elements of the stream, and then flattening the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   241
     * resulting elements into a new stream.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   242
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   243
     * <p><b>Examples.</b>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   244
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   245
     * <p>If {@code orders} is a stream of purchase orders, and each purchase
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   246
     * order contains a collection of line items, then the following produces a
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   247
     * stream containing all the line items in all the orders:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   248
     * <pre>{@code
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   249
     *     orders.flatMap(order -> order.getLineItems().stream())...
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   252
     * <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
   253
     * stream of the {@code words} contained in that file:
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   254
     * <pre>{@code
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   255
     *     Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   256
     *     Stream<String> words = lines.flatMap(line -> Stream.of(line.split(" +")));
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   257
     * }</pre>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   258
     * The {@code mapper} function passed to {@code flatMap} splits a line,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   259
     * using a simple regular expression, into an array of words, and then
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   260
     * creates a stream of words from that array.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   261
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   262
     * @param <R> The element type of the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   263
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   264
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   265
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   266
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   267
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   268
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   269
    <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   270
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   271
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   272
     * Returns an {@code IntStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   273
     * element of this stream with the contents of the stream produced by
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   274
     * applying the provided mapping function to each element.  (If the result
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   275
     * of the mapping function is {@code null}, this is treated as if the result
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   276
     * was an empty stream.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   277
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   278
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   279
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   280
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   281
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   282
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   283
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   284
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   285
     * @return the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   286
     * @see #flatMap(Function)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   287
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   288
    IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   289
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   290
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   291
     * Returns a {@code LongStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   292
     * element of this stream with the contents of the stream produced
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   293
     * by applying the provided mapping function to each element.  (If the result
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   294
     * of the mapping function is {@code null}, this is treated as if the result
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   295
     * was an empty stream.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   296
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   297
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   298
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   299
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   300
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   301
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   302
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   303
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   304
     * @return the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   305
     * @see #flatMap(Function)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   306
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   307
    LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   308
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   309
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   310
     * Returns a {@code DoubleStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   311
     * element of this stream with the contents of the stream produced
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   312
     * by applying the provided mapping function to each element.  (If the result
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   313
     * of the mapping function is {@code null}, this is treated as if the result
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   314
     * was an empty stream.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   315
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   316
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   317
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   318
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   319
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   320
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   321
     *               function to apply to each element which produces a stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   322
     *               of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   323
     * @return the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   324
     * @see #flatMap(Function)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   326
    DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   327
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   328
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   329
     * Returns a stream consisting of the distinct elements (according to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   330
     * {@link Object#equals(Object)}) of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   331
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   332
     * <p>For ordered streams, the selection of distinct elements is stable
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   333
     * (for duplicated elements, the element appearing first in the encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   334
     * order is preserved.)  For unordered streams, no stability guarantees
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   335
     * are made.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   336
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   337
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   338
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   339
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   340
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   341
     * Preserving stability for {@code distinct()} in parallel pipelines is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   342
     * relatively expensive (requires that the operation act as a full barrier,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   343
     * with substantial buffering overhead), and stability is often not needed.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   344
     * Using an unordered stream source (such as {@link #generate(Supplier)})
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   345
     * or removing the ordering constraint with {@link #unordered()} may result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   346
     * in significantly more efficient execution for {@code distinct()} in parallel
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   347
     * pipelines, if the semantics of your situation permit.  If consistency
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   348
     * with encounter order is required, and you are experiencing poor performance
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   349
     * or memory utilization with {@code distinct()} in parallel pipelines,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   350
     * switching to sequential execution with {@link #sequential()} may improve
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   351
     * performance.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   352
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   353
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   354
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   355
    Stream<T> distinct();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   356
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   357
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   358
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   359
     * according to natural order.  If the elements of this stream are not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   360
     * {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   361
     * when the terminal operation is executed.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   362
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   363
     * <p>For ordered streams, the sort is stable.  For unordered streams, no
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   364
     * stability guarantees are made.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   365
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   366
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   367
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   368
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   369
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   370
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   371
    Stream<T> sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   372
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   373
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   374
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   375
     * according to the provided {@code Comparator}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   376
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   377
     * <p>For ordered streams, the sort is stable.  For unordered streams, no
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   378
     * stability guarantees are made.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   379
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   380
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   381
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   382
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   383
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   384
     *                   <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   385
     *                   {@code Comparator} to be used to compare stream elements
17167
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(Comparator<? super T> comparator);
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, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   392
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   393
     * from the resulting stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   394
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   395
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   396
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   397
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   398
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   399
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   400
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   401
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   402
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   403
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   404
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   405
     * <pre>{@code
21846
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   406
     *     Stream.of("one", "two", "three", "four")
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   407
     *         .filter(e -> e.length() > 3)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   408
     *         .peek(e -> System.out.println("Filtered value: " + e))
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   409
     *         .map(String::toUpperCase)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   410
     *         .peek(e -> System.out.println("Mapped value: " + e))
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   411
     *         .collect(Collectors.toList());
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   414
     * @param action a <a href="package-summary.html#NonInterference">
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
     *                 non-interfering</a> action to perform on the elements as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
     *                 they are consumed from the stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   419
    Stream<T> peek(Consumer<? super T> action);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   423
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   424
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   425
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   426
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   427
     *
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   428
     * @apiNote
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   429
     * 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
   430
     * 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
   431
     * 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
   432
     * 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
   433
     * <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
   434
     * 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
   435
     * 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
   436
     * 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
   437
     * 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
   438
     * 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
   439
     * {@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
   440
     * with {@link #sequential()} may improve performance.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   441
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
     * @param maxSize the number of elements the stream should be limited to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
    Stream<T> limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   449
     * 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
   450
     * 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
   451
     * If this stream contains fewer than {@code n} elements then an
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   452
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   453
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   454
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   455
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   456
     *
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   457
     * @apiNote
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   458
     * 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
   459
     * 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
   460
     * 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
   461
     * 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
   462
     * <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
   463
     * 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
   464
     * 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
   465
     * 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
   466
     * 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
   467
     * 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
   468
     * {@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
   469
     * with {@link #sequential()} may improve performance.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   470
     *
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   471
     * @param n the number of leading elements to skip
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   472
     * @return the new stream
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   473
     * @throws IllegalArgumentException if {@code n} is negative
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     */
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19859
diff changeset
   475
    Stream<T> skip(long n);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   479
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   480
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   481
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   482
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   483
     * <p>The behavior of this operation is explicitly nondeterministic.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   484
     * For parallel stream pipelines, this operation does <em>not</em>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   485
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   486
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   487
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   488
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   489
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   490
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   491
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   492
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   493
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
    void forEach(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   495
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   496
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   497
     * Performs an action for each element of this stream, in the encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   498
     * order of the stream if the stream has a defined encounter order.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   499
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   500
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   501
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   502
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   503
     * <p>This operation processes the elements one at a time, in encounter
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   504
     * order if one exists.  Performing the action for one element
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   505
     * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   506
     * performing the action for subsequent elements, but for any given element,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   507
     * the action may be performed in whatever thread the library chooses.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   508
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   509
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   510
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   511
     * @see #forEach(Consumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   512
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   513
    void forEachOrdered(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   514
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   515
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   516
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   517
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   518
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   519
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   520
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   521
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   522
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   523
    Object[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   524
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   525
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   526
     * 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
   527
     * provided {@code generator} function to allocate the returned array, as
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   528
     * 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
   529
     * execution or for resizing.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   530
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   531
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   532
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   533
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   534
     * @apiNote
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   535
     * 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
   536
     * 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
   537
     * concisely expressed with an array constructor reference:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   538
     * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   539
     *     Person[] men = people.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   540
     *                          .filter(p -> p.getGender() == MALE)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   541
     *                          .toArray(Person[]::new);
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   542
     * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   543
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   544
     * @param <A> the element type of the resulting array
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   545
     * @param generator a function which produces a new array of the desired
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   546
     *                  type and the provided length
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   547
     * @return an array containing the elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   548
     * @throws ArrayStoreException if the runtime type of the array returned
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   549
     * from the array generator is not a supertype of the runtime type of every
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   550
     * element in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   551
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   552
    <A> A[] toArray(IntFunction<A[]> generator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   553
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   554
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   555
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   556
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   557
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   558
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   559
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   560
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   561
     *     T result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   562
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   563
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   564
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   565
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   566
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   567
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   568
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   569
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   570
     * function. This means that for all {@code t},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   571
     * {@code accumulator.apply(identity, t)} is equal to {@code t}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   572
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   573
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   574
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   575
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   576
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   577
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   578
     * @apiNote Sum, min, max, average, and string concatenation are all special
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   579
     * cases of reduction. Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   580
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   581
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   582
     *     Integer sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   583
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   584
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   585
     * or:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   586
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   587
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   588
     *     Integer sum = integers.reduce(0, Integer::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   589
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   590
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   591
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   592
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   593
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   594
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   595
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   596
     * @param identity the identity value for the accumulating function
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   597
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   598
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   599
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   600
     *                    function for combining two values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   601
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   602
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   603
    T reduce(T identity, BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   604
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   605
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   606
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   607
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   608
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   609
     * function, and returns an {@code Optional} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   610
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   611
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   612
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   613
     *     T result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   614
     *     for (T element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   615
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   616
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   617
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   618
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   619
     *         else
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   620
     *             result = accumulator.apply(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   621
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   622
     *     return foundAny ? Optional.of(result) : Optional.empty();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   623
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   624
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   625
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   626
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   627
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   628
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   629
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   630
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   633
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   634
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   635
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   636
     *                    function for combining two values
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   637
     * @return an {@link Optional} describing the result of the reduction
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   638
     * @throws NullPointerException if the result of the reduction is null
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   639
     * @see #reduce(Object, BinaryOperator)
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   640
     * @see #min(Comparator)
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   641
     * @see #max(Comparator)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   643
    Optional<T> reduce(BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   644
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   645
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   646
     * 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
   647
     * 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
   648
     * combining functions.  This is equivalent to:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   649
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   650
     *     U result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   651
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   652
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   653
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   654
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   655
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   656
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   657
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   658
     * <p>The {@code identity} value must be an identity for the combiner
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   659
     * function.  This means that for all {@code u}, {@code combiner(identity, u)}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   660
     * is equal to {@code u}.  Additionally, the {@code combiner} function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   661
     * must be compatible with the {@code accumulator} function; for all
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   662
     * {@code u} and {@code t}, the following must hold:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   663
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   664
     *     combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   665
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   666
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   667
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   668
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   669
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   670
     * @apiNote Many reductions using this form can be represented more simply
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   671
     * by an explicit combination of {@code map} and {@code reduce} operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   672
     * The {@code accumulator} function acts as a fused mapper and accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   673
     * 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
   674
     * 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
   675
     * some computation.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   676
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   677
     * @param <U> The type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   678
     * @param identity the identity value for the combiner function
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   679
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   680
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   681
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   682
     *                    function for incorporating an additional element into a result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   683
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   684
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   685
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   686
     *                    function for combining two values, which must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   687
     *                    compatible with the accumulator function
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   688
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   689
     * @see #reduce(BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   690
     * @see #reduce(Object, BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   691
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   692
    <U> U reduce(U identity,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   693
                 BiFunction<U, ? super T, U> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   694
                 BinaryOperator<U> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   695
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   696
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   697
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   698
     * 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
   699
     * reduction is one in which the reduced value is a mutable result container,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   700
     * 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
   701
     * the state of the result rather than by replacing the result.  This
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   702
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   703
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   704
     *     R result = supplier.get();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   705
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   706
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   707
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   708
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   709
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   710
     * <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   711
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   712
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   713
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   714
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   715
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   716
     * @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
   717
     * 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
   718
     * For example, the following will accumulate strings into an {@code ArrayList}:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   719
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   720
     *     List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   721
     *                                                ArrayList::addAll);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   722
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   723
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   724
     * <p>The following will take a stream of strings and concatenates them into a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   725
     * single string:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   726
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   727
     *     String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   728
     *                                          StringBuilder::append)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   729
     *                                 .toString();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   730
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   731
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   732
     * @param <R> type of the result
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   733
     * @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
   734
     *                 parallel execution, this function may be called
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   735
     *                 multiple times and must return a fresh value each time.
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   736
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   737
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   738
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   739
     *                    function for incorporating an additional element into a result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   740
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   741
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   742
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   743
     *                    function for combining two values, which must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   744
     *                    compatible with the accumulator function
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   745
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   746
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   747
    <R> R collect(Supplier<R> supplier,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   748
                  BiConsumer<R, ? super T> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   749
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   750
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   751
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   752
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   753
     * 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
   754
     * {@code Collector}.  A {@code Collector}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   755
     * encapsulates the functions used as arguments to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   756
     * {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   757
     * collection strategies and composition of collect operations such as
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   758
     * multiple-level grouping or partitioning.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   759
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   760
     * <p>If the stream is parallel, and the {@code Collector}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   761
     * is {@link Collector.Characteristics#CONCURRENT concurrent}, and
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   762
     * either the stream is unordered or the collector is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   763
     * {@link Collector.Characteristics#UNORDERED unordered},
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   764
     * then a concurrent reduction will be performed (see {@link Collector} for
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   765
     * details on concurrent reduction.)
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   766
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   767
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   768
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   769
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   770
     * <p>When executed in parallel, multiple intermediate results may be
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   771
     * instantiated, populated, and merged so as to maintain isolation of
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   772
     * mutable data structures.  Therefore, even when executed in parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   773
     * with non-thread-safe data structures (such as {@code ArrayList}), no
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   774
     * additional synchronization is needed for a parallel reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   775
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   776
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   777
     * The following will accumulate strings into an ArrayList:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   778
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   779
     *     List<String> asList = stringStream.collect(Collectors.toList());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   780
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   781
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   782
     * <p>The following will classify {@code Person} objects by city:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   783
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   784
     *     Map<String, List<Person>> peopleByCity
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   785
     *         = personStream.collect(Collectors.groupingBy(Person::getCity));
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   786
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   787
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   788
     * <p>The following will classify {@code Person} objects by state and city,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   789
     * cascading two {@code Collector}s together:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   790
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   791
     *     Map<String, Map<String, List<Person>>> peopleByStateAndCity
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   792
     *         = personStream.collect(Collectors.groupingBy(Person::getState,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   793
     *                                                      Collectors.groupingBy(Person::getCity)));
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   794
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   795
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   796
     * @param <R> the type of the result
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19199
diff changeset
   797
     * @param <A> the intermediate accumulation type of the {@code Collector}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   798
     * @param collector the {@code Collector} describing the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   799
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   800
     * @see #collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   801
     * @see Collectors
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   802
     */
19799
efa9ff09b024 8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos
henryjen
parents: 19214
diff changeset
   803
    <R, A> R collect(Collector<? super T, A, R> collector);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   804
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   805
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   806
     * Returns the minimum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   807
     * {@code Comparator}.  This is a special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   808
     * <a href="package-summary.html#Reduction">reduction</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   809
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   810
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   811
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   812
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   813
     *                   <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   814
     *                   {@code Comparator} to compare elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   815
     * @return an {@code Optional} describing the minimum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   816
     * or an empty {@code Optional} if the stream is empty
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   817
     * @throws NullPointerException if the minimum element is null
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   818
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   819
    Optional<T> min(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   820
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   821
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   822
     * Returns the maximum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   823
     * {@code Comparator}.  This is a special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   824
     * <a href="package-summary.html#Reduction">reduction</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   825
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   826
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   827
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   828
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   829
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   830
     *                   <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   831
     *                   {@code Comparator} to compare elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   832
     * @return an {@code Optional} describing the maximum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   833
     * or an empty {@code Optional} if the stream is empty
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   834
     * @throws NullPointerException if the maximum element is null
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   835
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   836
    Optional<T> max(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   837
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   838
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   839
     * 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
   840
     * a <a href="package-summary.html#Reduction">reduction</a> and is
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   841
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   842
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   843
     *     return mapToLong(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   844
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   845
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   846
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   847
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   848
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   849
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   850
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   851
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   852
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   853
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   854
     * predicate.  May not evaluate the predicate on all elements if not
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   855
     * necessary for determining the result.  If the stream is empty then
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   856
     * {@code false} is returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   857
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   858
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   859
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   860
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   861
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   862
     * This method evaluates the <em>existential quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   863
     * predicate over the elements of the stream (for some x P(x)).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   864
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   865
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   866
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   867
     *                  predicate to apply to elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   868
     * @return {@code true} if any elements of the stream match the provided
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   869
     * predicate, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   870
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   871
    boolean anyMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   872
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   873
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   874
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   875
     * May not evaluate the predicate on all elements if not necessary for
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   876
     * determining the result.  If the stream is empty then {@code true} is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   877
     * returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   878
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   879
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   880
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   881
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   882
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   883
     * This method evaluates the <em>universal quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   884
     * 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
   885
     * stream is empty, the quantification is said to be <em>vacuously
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   886
     * satisfied</em> and is always {@code true} (regardless of P(x)).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   887
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   888
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   889
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   890
     *                  predicate to apply to elements of this stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   891
     * @return {@code true} if either all elements of the stream match the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   892
     * provided predicate or the stream is empty, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   893
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   894
    boolean allMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   895
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   896
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   897
     * Returns whether no elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   898
     * May not evaluate the predicate on all elements if not necessary for
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   899
     * determining the result.  If the stream is empty then {@code true} is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   900
     * returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   901
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   902
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   903
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   904
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   905
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   906
     * This method evaluates the <em>universal quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   907
     * 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
   908
     * the stream is empty, the quantification is said to be vacuously satisfied
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   909
     * and is always {@code true}, regardless of P(x).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   910
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   911
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   912
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   913
     *                  predicate to apply to elements of this stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   914
     * @return {@code true} if either no elements of the stream match the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   915
     * provided predicate or the stream is empty, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   916
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   917
    boolean noneMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   918
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   919
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   920
     * Returns an {@link Optional} describing the first element of this stream,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   921
     * 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
   922
     * no encounter order, then any element may be returned.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   923
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   924
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   925
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   926
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   927
     * @return an {@code Optional} describing the first element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   928
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   929
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   930
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   931
    Optional<T> findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   932
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   933
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   934
     * Returns an {@link Optional} describing some element of the stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   935
     * empty {@code Optional} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   936
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   937
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   938
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   939
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   940
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   941
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   942
     * performance in parallel operations; the cost is that multiple invocations
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   943
     * 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
   944
     * is desired, use {@link #findFirst()} instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   945
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   946
     * @return an {@code Optional} describing some element of this stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   947
     * empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   948
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   949
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   950
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   951
    Optional<T> findAny();
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   952
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   953
    // Static factories
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   954
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   955
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   956
     * Returns a builder for a {@code Stream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   957
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   958
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   959
     * @return a stream builder
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   960
     */
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   961
    public static<T> Builder<T> builder() {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   962
        return new Streams.StreamBuilderImpl<>();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   963
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   964
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   965
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   966
     * Returns an empty sequential {@code Stream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   967
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   968
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   969
     * @return an empty sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   970
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   971
    public static<T> Stream<T> empty() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   972
        return StreamSupport.stream(Spliterators.<T>emptySpliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   973
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   974
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   975
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   976
     * Returns a sequential {@code Stream} containing a single element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   977
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   978
     * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   979
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   980
     * @return a singleton sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   981
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   982
    public static<T> Stream<T> of(T t) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   983
        return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   984
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   985
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   986
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   987
     * Returns a sequential ordered stream whose elements are the specified values.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   988
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   989
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   990
     * @param values the elements of the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   991
     * @return the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   992
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   993
    @SafeVarargs
19859
ac48498acd3a 8024825: Some fixes are missing from java.util.stream spec update
henryjen
parents: 19850
diff changeset
   994
    @SuppressWarnings("varargs") // Creating a stream from an array is safe
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   995
    public static<T> Stream<T> of(T... values) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   996
        return Arrays.stream(values);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   997
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   998
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   999
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1000
     * Returns an infinite sequential ordered {@code Stream} produced by iterative
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1001
     * application of a function {@code f} to an initial element {@code seed},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1002
     * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1003
     * {@code f(f(seed))}, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1004
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1005
     * <p>The first element (position {@code 0}) in the {@code Stream} will be
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1006
     * the provided {@code seed}.  For {@code n > 0}, the element at position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1007
     * {@code n}, will be the result of applying the function {@code f} to the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1008
     * element at position {@code n - 1}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1009
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1010
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1011
     * @param seed the initial element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1012
     * @param f a function to be applied to to the previous element to produce
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1013
     *          a new element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1014
     * @return a new sequential {@code Stream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1015
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1016
    public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1017
        Objects.requireNonNull(f);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1018
        final Iterator<T> iterator = new Iterator<T>() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1019
            @SuppressWarnings("unchecked")
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1020
            T t = (T) Streams.NONE;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1021
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1022
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1023
            public boolean hasNext() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1024
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1025
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1026
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1027
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1028
            public T next() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1029
                return t = (t == Streams.NONE) ? seed : f.apply(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1030
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1031
        };
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1032
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1033
                iterator,
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1034
                Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1035
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1036
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1037
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1038
     * Returns an infinite sequential unordered stream where each element is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1039
     * generated by the provided {@code Supplier}.  This is suitable for
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1040
     * generating constant streams, streams of random elements, etc.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1041
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1042
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1043
     * @param s the {@code Supplier} of generated elements
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1044
     * @return a new infinite sequential unordered {@code Stream}
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1045
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1046
    public static<T> Stream<T> generate(Supplier<T> s) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1047
        Objects.requireNonNull(s);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17914
diff changeset
  1048
        return StreamSupport.stream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1049
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1050
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1051
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1052
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1053
     * Creates a lazily concatenated stream whose elements are all the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1054
     * elements of the first stream followed by all the elements of the
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1055
     * second stream.  The resulting stream is ordered if both
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1056
     * 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
  1057
     * streams is parallel.  When the resulting stream is closed, the close
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1058
     * handlers for both input streams are invoked.
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1059
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1060
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1061
     * Use caution when constructing streams from repeated concatenation.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1062
     * Accessing an element of a deeply concatenated stream can result in deep
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1063
     * call chains, or even {@code StackOverflowException}.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1064
     *
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1065
     * @param <T> The type of stream elements
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1066
     * @param a the first stream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1067
     * @param b the second stream
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1068
     * @return the concatenation of the two input streams
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1069
     */
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1070
    public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1071
        Objects.requireNonNull(a);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1072
        Objects.requireNonNull(b);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1073
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1074
        @SuppressWarnings("unchecked")
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1075
        Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1076
                (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1077
        Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19799
diff changeset
  1078
        return stream.onClose(Streams.composedClose(a, b));
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1079
    }
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1080
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1081
    /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1082
     * 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
  1083
     * {@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
  1084
     * {@code Builder} (without the copying overhead that comes from using
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1085
     * an {@code ArrayList} as a temporary buffer.)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1086
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1087
     * <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
  1088
     * 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
  1089
     * 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
  1090
     * 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
  1091
     * {@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
  1092
     * builder, in the order they were added.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1093
     *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1094
     * @param <T> the type of stream elements
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1095
     * @see Stream#builder()
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1096
     * @since 1.8
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1097
     */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1098
    public interface Builder<T> extends Consumer<T> {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1099
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1100
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1101
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1102
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1103
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1104
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1105
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1106
        @Override
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1107
        void accept(T t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1108
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1109
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1110
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1111
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1112
         * @implSpec
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1113
         * The default implementation behaves as if:
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1114
         * <pre>{@code
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1115
         *     accept(t)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1116
         *     return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1117
         * }</pre>
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1118
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1119
         * @param t the element to add
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1120
         * @return {@code this} builder
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1121
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1122
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1123
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1124
        default Builder<T> add(T t) {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1125
            accept(t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1126
            return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1127
        }
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1128
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1129
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1130
         * Builds the stream, transitioning this builder to the built state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1131
         * An {@code IllegalStateException} is thrown if there are further attempts
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1132
         * 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
  1133
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1134
         * @return the built stream
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1135
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1136
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1137
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1138
        Stream<T> build();
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1139
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1140
    }
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1141
}