jdk/src/share/classes/java/util/stream/Stream.java
author henryjen
Tue, 03 Sep 2013 11:44:34 -0700
changeset 19799 efa9ff09b024
parent 19214 e5901820c3c1
child 19800 6e1fef53ea55
permissions -rw-r--r--
8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos Reviewed-by: mduigou
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
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    27
import java.util.Arrays;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    28
import java.util.Comparator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    29
import java.util.Iterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    30
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    31
import java.util.Optional;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    32
import java.util.Spliterator;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    33
import java.util.Spliterators;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    34
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
import java.util.function.BiFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    36
import java.util.function.BinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
import java.util.function.Consumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    38
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    39
import java.util.function.IntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
import java.util.function.Predicate;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    41
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
import java.util.function.ToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
import java.util.function.ToIntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
import java.util.function.ToLongFunction;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    45
import java.util.function.UnaryOperator;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    46
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    47
// @@@ Specification to-do list @@@
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    48
// - Describe the difference between sequential and parallel streams
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    49
// - More general information about reduce, better definitions for associativity, more description of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    50
//   how reduce employs parallelism, more examples
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    51
// - Role of stream flags in various operations, specifically ordering
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    52
//   - Whether each op preserves encounter order
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    53
// @@@ Specification to-do list @@@
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    54
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    55
/**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    56
 * A sequence of elements supporting sequential and parallel bulk operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    57
 * Streams support lazy intermediate operations (transforming a stream to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    58
 * another stream) such as {@code filter} and {@code map}, and terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    59
 * operations (consuming the contents of a stream to produce a result or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    60
 * side-effect), such as {@code forEach}, {@code findFirst}, and {@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    61
 * iterator}.  Once an operation has been performed on a stream, it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    62
 * is considered <em>consumed</em> and no longer usable for other operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    63
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    64
 * <p>For sequential stream pipelines, all operations are performed in the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    65
 * <a href="package-summary.html#Ordering">encounter order</a> of the pipeline
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    66
 * source, if the pipeline source has a defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    67
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    68
 * <p>For parallel stream pipelines, unless otherwise specified, intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    69
 * stream operations preserve the <a href="package-summary.html#Ordering">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    70
 * encounter order</a> of their source, and terminal operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    71
 * respect the encounter order of their source, if the source
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    72
 * has an encounter order.  Provided that and parameters to stream operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    73
 * satisfy the <a href="package-summary.html#NonInterference">non-interference
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    74
 * requirements</a>, and excepting differences arising from the absence of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    75
 * a defined encounter order, the result of a stream pipeline should be the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    76
 * stable across multiple executions of the same operations on the same source.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    77
 * However, the timing and thread in which side-effects occur (for those
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    78
 * operations which are allowed to produce side-effects, such as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    79
 * {@link #forEach(Consumer)}), are explicitly nondeterministic for parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    80
 * execution of stream pipelines.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    81
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    82
 * <p>Unless otherwise noted, passing a {@code null} argument to any stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    83
 * method may result in a {@link NullPointerException}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    84
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    85
 * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    86
 * Streams are not data structures; they do not manage the storage for their
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    87
 * elements, nor do they support access to individual elements.  However,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    88
 * you can use the {@link #iterator()} or {@link #spliterator()} operations to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    89
 * perform a controlled traversal.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    90
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    91
 * @param <T> type of elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    92
 * @since 1.8
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    93
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    94
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    95
public interface Stream<T> extends BaseStream<T, Stream<T>> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    96
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    97
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    98
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    99
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   100
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   101
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   102
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   103
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   104
     * @param predicate a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   105
     *                  non-interfering, stateless</a> predicate to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   106
     *                  each element to determine if it should be included
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   107
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   108
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
    Stream<T> filter(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   110
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   111
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   112
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   113
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   114
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   115
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   116
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   118
     * @param <R> The element type of the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   119
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   120
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   121
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   122
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   123
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   124
    <R> Stream<R> map(Function<? super T, ? extends R> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   125
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   126
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   127
     * Returns an {@code IntStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   128
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   129
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   130
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   131
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   132
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   133
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   134
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   135
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   137
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   138
    IntStream mapToInt(ToIntFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   140
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   141
     * Returns a {@code LongStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   142
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   143
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   144
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   145
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   146
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   147
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   148
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   149
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   150
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   151
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   152
    LongStream mapToLong(ToLongFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   153
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   154
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   155
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   156
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   158
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   159
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   161
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   163
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   164
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
    DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
     * Returns a stream consisting of the results of replacing each element of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
     * this stream with the contents of the stream produced by applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
     * provided mapping function to each element.  If the result of the mapping
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     * function is {@code null}, this is treated as if the result is an empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * 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
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   179
     * The {@code flatMap()} operation has the effect of applying a one-to-many
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   180
     * tranformation to the elements of the stream, and then flattening the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   181
     * resulting elements into a new stream. For example, if {@code orders}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
     * is a stream of purchase orders, and each purchase order contains a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
     * collection of line items, then the following produces a stream of line
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
     * items:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
     *     orderStream.flatMap(order -> order.getLineItems().stream())...
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
     * @param <R> The element type of the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
     *               element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   193
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   194
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   195
    <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   196
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   197
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   198
     * Returns an {@code IntStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
     * element of this stream with the contents of the stream produced by
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
     * applying the provided mapping function to each element.  If the result of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   201
     * the mapping function is {@code null}, this is treated as if the result is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
     * an empty 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
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   207
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   208
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   209
     *               element which produces a stream of new values
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
    IntStream flatMapToInt(Function<? super T, ? extends IntStream> 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 LongStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   216
     * element of this stream with the contents of the stream produced
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   217
     * by applying the provided mapping function to each element.  If the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     * of the mapping function is {@code null}, this is treated as if the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   219
     * result is an empty stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   221
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   222
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
     *               non-interfering, stateless</a> function to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
     *               each element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
    LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   231
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   232
     * Returns a {@code DoubleStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   233
     * element of this stream with the contents of the stream produced
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   234
     * by applying the provided mapping function to each element.  If the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   235
     * of the mapping function is {@code null}, this is treated as if the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   236
     * is an empty stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   237
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   240
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   241
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   242
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   243
     *               element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   244
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   245
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   246
    DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   247
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   248
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   249
     * Returns a stream consisting of the distinct elements (according to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
     * {@link Object#equals(Object)}) of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   252
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   253
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   254
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   255
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   256
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   257
    Stream<T> distinct();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   258
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   259
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   260
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   261
     * according to natural order.  If the elements of this stream are not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   262
     * {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   263
     * when the stream pipeline is executed.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   264
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   265
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   266
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   267
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   268
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   269
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   270
    Stream<T> sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   271
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   272
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   273
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   274
     * according to the provided {@code Comparator}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   275
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   276
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   277
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   278
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   279
     * @param comparator a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   280
     *                   non-interfering, stateless</a> {@code Comparator} to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   281
     *                   be used to compare stream elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   282
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   283
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   284
    Stream<T> sorted(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   285
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   286
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   287
     * Returns a stream consisting of the elements of this stream, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   288
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   289
     * from the resulting stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   290
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   291
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   292
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   293
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   294
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   295
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   296
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   297
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   298
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   299
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   300
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   301
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   302
     *     list.stream()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   303
     *         .filter(filteringFunction)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   304
     *         .peek(e -> {System.out.println("Filtered value: " + e); });
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   305
     *         .map(mappingFunction)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   306
     *         .peek(e -> {System.out.println("Mapped value: " + e); });
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   307
     *         .collect(Collectors.intoList());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   308
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   309
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   310
     * @param consumer a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   311
     *                 non-interfering</a> action to perform on the elements as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   312
     *                 they are consumed from the stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   313
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   314
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   315
    Stream<T> peek(Consumer<? super T> consumer);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   316
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   317
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   318
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   319
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   320
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   321
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   322
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   323
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   324
     * @param maxSize the number of elements the stream should be limited to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   326
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   327
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   328
    Stream<T> limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   329
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   330
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   331
     * Returns a stream consisting of the remaining elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   332
     * after indexing {@code startInclusive} elements into the stream. If the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   333
     * {@code startInclusive} index lies past the end of this stream then an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   334
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   335
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   336
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   337
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   338
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   339
     * @param startInclusive the number of leading elements to skip
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   340
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   341
     * @throws IllegalArgumentException if {@code startInclusive} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   342
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   343
    Stream<T> substream(long startInclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   344
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   345
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   346
     * Returns a stream consisting of the remaining elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   347
     * after indexing {@code startInclusive} elements into the stream and
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   348
     * truncated to contain no more than {@code endExclusive - startInclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   349
     * elements. If the {@code startInclusive} index lies past the end
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   350
     * of this stream then an empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   351
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   352
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   353
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   354
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   355
     * @param startInclusive the starting position of the substream, inclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   356
     * @param endExclusive the ending position of the substream, exclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   357
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   358
     * @throws IllegalArgumentException if {@code startInclusive} or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   359
     * {@code endExclusive} is negative or {@code startInclusive} is greater
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   360
     * than {@code endExclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   361
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   362
    Stream<T> substream(long startInclusive, long endExclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   363
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   364
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   365
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   366
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   367
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   368
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   369
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   370
     * <p>For parallel stream pipelines, this operation does <em>not</em>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   371
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   372
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   373
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   374
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   375
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   376
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   377
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   378
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   379
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   380
    void forEach(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   381
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   382
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   383
     * Performs an action for each element of this stream, guaranteeing that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   384
     * each element is processed in encounter order for streams that have a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   385
     * defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   386
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   387
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   388
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   389
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   390
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   391
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   392
     * @see #forEach(Consumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   393
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   394
    void forEachOrdered(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   395
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   396
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   397
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   398
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   399
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   400
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   401
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   402
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   403
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   404
    Object[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   405
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   406
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   407
     * Returns an array containing the elements of this stream, using the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   408
     * provided {@code generator} function to allocate the returned array.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   409
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   410
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   411
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     * @param <A> the element type of the resulting array
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   414
     * @param generator a function which produces a new array of the desired
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
     *                  type and the provided length
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
     * @return an array containing the elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
     * @throws ArrayStoreException if the runtime type of the array returned
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
     * from the array generator is not a supertype of the runtime type of every
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   419
     * element in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
    <A> A[] toArray(IntFunction<A[]> generator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   423
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   424
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   425
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   426
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   427
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   428
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   429
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   430
     *     T result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   431
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   432
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   433
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   434
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   435
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   436
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   437
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   438
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   439
     * function. This means that for all {@code t},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   440
     * {@code accumulator.apply(identity, t)} is equal to {@code t}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   441
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
     * @apiNote Sum, min, max, average, and string concatenation are all special
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
     * cases of reduction. Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   449
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   450
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   451
     *     Integer sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   452
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   453
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   454
     * or more compactly:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   455
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   456
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   457
     *     Integer sum = integers.reduce(0, Integer::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   458
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   459
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   460
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   461
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   462
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   463
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   464
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   465
     * @param identity the identity value for the accumulating function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   466
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   467
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   468
     *                    stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   469
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   470
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   471
    T reduce(T identity, BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   472
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   473
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   475
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
     * function, and returns an {@code Optional} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   479
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   480
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   481
     *     T result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   482
     *     for (T element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   483
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   484
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   485
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   486
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   487
     *         else
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   488
     *             result = accumulator.apply(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   489
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   490
     *     return foundAny ? Optional.of(result) : Optional.empty();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   491
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   492
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   493
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   495
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   496
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   497
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   498
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   499
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   500
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   501
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   502
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   503
     *                    stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   504
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   505
     * @see #reduce(Object, BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   506
     * @see #min(java.util.Comparator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   507
     * @see #max(java.util.Comparator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   508
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   509
    Optional<T> reduce(BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   510
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   511
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   512
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   513
     * elements of this stream, using the provided identity, accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   514
     * function, and a combining functions.  This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   515
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   516
     *     U result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   517
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   518
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   519
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   520
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   521
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   522
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   523
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   524
     * <p>The {@code identity} value must be an identity for the combiner
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   525
     * function.  This means that for all {@code u}, {@code combiner(identity, u)}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   526
     * is equal to {@code u}.  Additionally, the {@code combiner} function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   527
     * must be compatible with the {@code accumulator} function; for all
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   528
     * {@code u} and {@code t}, the following must hold:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   529
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   530
     *     combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   531
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   532
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   533
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   534
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   535
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   536
     * @apiNote Many reductions using this form can be represented more simply
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   537
     * by an explicit combination of {@code map} and {@code reduce} operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   538
     * The {@code accumulator} function acts as a fused mapper and accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   539
     * which can sometimes be more efficient than separate mapping and reduction,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   540
     * such as in the case where knowing the previously reduced value allows you
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   541
     * to avoid some computation.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   542
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   543
     * @param <U> The type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   544
     * @param identity the identity value for the combiner function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   545
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   546
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   547
     *                    stateless</a> function for incorporating an additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   548
     *                    element into a result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   549
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   550
     *                 <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   551
     *                 stateless</a> function for combining two values, which
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   552
     *                 must be compatible with the accumulator function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   553
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   554
     * @see #reduce(BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   555
     * @see #reduce(Object, BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   556
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   557
    <U> U reduce(U identity,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   558
                 BiFunction<U, ? super T, U> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   559
                 BinaryOperator<U> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   560
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   561
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   562
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   563
     * reduction</a> operation on the elements of this stream.  A mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   564
     * reduction is one in which the reduced value is a mutable value holder,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   565
     * such as an {@code ArrayList}, and elements are incorporated by updating
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   566
     * the state of the result, rather than by replacing the result.  This
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   567
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   568
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   569
     *     R result = resultFactory.get();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   570
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   571
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   572
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   573
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   574
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   575
     * <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   576
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   577
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   578
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   579
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   580
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   581
     * @apiNote There are many existing classes in the JDK whose signatures are
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   582
     * a good match for use as arguments to {@code collect()}.  For example,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   583
     * the following will accumulate strings into an ArrayList:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   584
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   585
     *     List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   586
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   587
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   588
     * <p>The following will take a stream of strings and concatenates them into a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   589
     * single string:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   590
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   591
     *     String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   592
     *                                          StringBuilder::append)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   593
     *                                 .toString();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   594
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   595
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   596
     * @param <R> type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   597
     * @param resultFactory a function that creates a new result container.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   598
     *                      For a parallel execution, this function may be
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   599
     *                      called multiple times and must return a fresh value
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   600
     *                      each time.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   601
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   602
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   603
     *                    stateless</a> function for incorporating an additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   604
     *                    element into a result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   605
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   606
     *                 <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   607
     *                 stateless</a> function for combining two values, which
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   608
     *                 must be compatible with the accumulator function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   609
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   610
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   611
    <R> R collect(Supplier<R> resultFactory,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   612
                  BiConsumer<R, ? super T> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   613
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   614
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   615
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   616
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   617
     * reduction</a> operation on the elements of this stream using a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   618
     * {@code Collector} object to describe the reduction.  A {@code Collector}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   619
     * encapsulates the functions used as arguments to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   620
     * {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   621
     * collection strategies, and composition of collect operations such as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   622
     * multiple-level grouping or partitioning.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   623
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   624
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   625
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   626
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   627
     * <p>When executed in parallel, multiple intermediate results may be
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   628
     * instantiated, populated, and merged, so as to maintain isolation of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   629
     * mutable data structures.  Therefore, even when executed in parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   630
     * with non-thread-safe data structures (such as {@code ArrayList}), no
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
     * additional synchronization is needed for a parallel reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   633
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   634
     * The following will accumulate strings into an ArrayList:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   635
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   636
     *     List<String> asList = stringStream.collect(Collectors.toList());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   637
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   638
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   639
     * <p>The following will classify {@code Person} objects by city:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   640
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   641
     *     Map<String, Collection<Person>> peopleByCity
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
     *         = personStream.collect(Collectors.groupBy(Person::getCity));
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   643
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   644
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   645
     * <p>The following will classify {@code Person} objects by state and city,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   646
     * cascading two {@code Collector}s together:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   647
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   648
     *     Map<String, Map<String, Collection<Person>>> peopleByStateAndCity
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   649
     *         = personStream.collect(Collectors.groupBy(Person::getState,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   650
     *                                                   Collectors.groupBy(Person::getCity)));
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   651
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   652
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   653
     * @param <R> the type of the result
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 19199
diff changeset
   654
     * @param <A> the intermediate accumulation type of the {@code Collector}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   655
     * @param collector the {@code Collector} describing the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   656
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   657
     * @see #collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   658
     * @see Collectors
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   659
     */
19799
efa9ff09b024 8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos
henryjen
parents: 19214
diff changeset
   660
    <R, A> R collect(Collector<? super T, A, R> collector);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   661
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   662
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   663
     * Returns the minimum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   664
     * {@code Comparator}.  This is a special case of a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   665
     * <a href="package-summary.html#MutableReduction">reduction</a>.
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 operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   668
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   669
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   670
     *                   stateless</a> {@code Comparator} to use to compare
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   671
     *                   elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   672
     * @return an {@code Optional} describing the minimum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   673
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   674
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   675
    Optional<T> min(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   676
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   677
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   678
     * Returns the maximum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   679
     * {@code Comparator}.  This is a special case of a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   680
     * <a href="package-summary.html#MutableReduction">reduction</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   681
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   682
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   683
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   684
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   685
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   686
     *                   stateless</a> {@code Comparator} to use to compare
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   687
     *                   elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   688
     * @return an {@code Optional} describing the maximum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   689
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   690
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   691
    Optional<T> max(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   692
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   693
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   694
     * Returns the count of elements in this stream.  This is a special case of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   695
     * a <a href="package-summary.html#MutableReduction">reduction</a> and is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   696
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   697
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   698
     *     return mapToLong(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   699
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   700
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   701
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   702
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   703
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   704
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   705
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   706
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   707
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   708
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   709
     * predicate.  May not evaluate the predicate on all elements if not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   710
     * necessary for determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   711
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   712
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   713
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   714
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   715
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   716
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   717
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   718
     * @return {@code true} if any elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   719
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   720
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   721
    boolean anyMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   722
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   723
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   724
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   725
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   726
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   727
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   728
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   729
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   730
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   731
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   732
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   733
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   734
     * @return {@code true} if all elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   735
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   736
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   737
    boolean allMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   738
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   739
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   740
     * Returns whether no elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   741
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   742
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   743
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   744
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   745
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   746
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   747
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   748
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   749
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   750
     * @return {@code true} if no elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   751
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   752
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   753
    boolean noneMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   754
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   755
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   756
     * Returns an {@link Optional} describing the first element of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   757
     * (in the encounter order), or an empty {@code Optional} if the stream is
17914
91e138d3b298 8014393: Minor typo in the spec for j.u.stream.Stream.findFirst()
psandoz
parents: 17195
diff changeset
   758
     * empty.  If the stream has no encounter order, then any element may be
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   759
     * returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   760
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   761
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   762
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   763
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   764
     * @return an {@code Optional} describing the first element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   765
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   766
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   767
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   768
    Optional<T> findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   769
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   770
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   771
     * Returns an {@link Optional} describing some element of the stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   772
     * empty {@code Optional} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   773
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   774
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   775
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   776
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   777
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   778
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   779
     * performance in parallel operations; the cost is that multiple invocations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   780
     * on the same source may not return the same result.  (If the first element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   781
     * in the encounter order is desired, use {@link #findFirst()} instead.)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   782
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   783
     * @return an {@code Optional} describing some element of this stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   784
     * empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   785
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   786
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   787
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   788
    Optional<T> findAny();
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   789
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   790
    // Static factories
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   791
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   792
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   793
     * Returns a builder for a {@code Stream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   794
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   795
     * @param <T> type of elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   796
     * @return a stream builder
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   797
     */
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   798
    public static<T> Builder<T> builder() {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   799
        return new Streams.StreamBuilderImpl<>();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   800
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   801
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   802
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   803
     * Returns an empty sequential {@code Stream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   804
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   805
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   806
     * @return an empty sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   807
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   808
    public static<T> Stream<T> empty() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   809
        return StreamSupport.stream(Spliterators.<T>emptySpliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   810
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   811
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   812
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   813
     * Returns a sequential {@code Stream} containing a single element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   814
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   815
     * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   816
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   817
     * @return a singleton sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   818
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   819
    public static<T> Stream<T> of(T t) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   820
        return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   821
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   822
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   823
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   824
     * Returns a sequential stream whose elements are the specified values.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   825
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   826
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   827
     * @param values the elements of the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   828
     * @return the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   829
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   830
    @SafeVarargs
19199
4dfcc94aa1f2 8022190: Fix varargs lint warnings in the JDK
darcy
parents: 18825
diff changeset
   831
    @SuppressWarnings("varargs") // Creating a stream from an array is safe
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   832
    public static<T> Stream<T> of(T... values) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   833
        return Arrays.stream(values);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   834
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   835
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   836
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   837
     * Returns an infinite sequential {@code Stream} produced by iterative
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   838
     * application of a function {@code f} to an initial element {@code seed},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   839
     * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   840
     * {@code f(f(seed))}, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   841
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   842
     * <p>The first element (position {@code 0}) in the {@code Stream} will be
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   843
     * the provided {@code seed}.  For {@code n > 0}, the element at position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   844
     * {@code n}, will be the result of applying the function {@code f} to the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   845
     * element at position {@code n - 1}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   846
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   847
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   848
     * @param seed the initial element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   849
     * @param f a function to be applied to to the previous element to produce
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   850
     *          a new element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   851
     * @return a new sequential {@code Stream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   852
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   853
    public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   854
        Objects.requireNonNull(f);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   855
        final Iterator<T> iterator = new Iterator<T>() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   856
            @SuppressWarnings("unchecked")
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   857
            T t = (T) Streams.NONE;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   858
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   859
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   860
            public boolean hasNext() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   861
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   862
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   863
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   864
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   865
            public T next() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   866
                return t = (t == Streams.NONE) ? seed : f.apply(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   867
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   868
        };
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   869
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   870
                iterator,
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   871
                Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   872
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   873
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   874
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   875
     * Returns a sequential {@code Stream} where each element is
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   876
     * generated by a {@code Supplier}.  This is suitable for generating
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   877
     * constant streams, streams of random elements, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   878
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   879
     * @param <T> the type of stream elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   880
     * @param s the {@code Supplier} of generated elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   881
     * @return a new sequential {@code Stream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   882
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   883
    public static<T> Stream<T> generate(Supplier<T> s) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   884
        Objects.requireNonNull(s);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 17914
diff changeset
   885
        return StreamSupport.stream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   886
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   887
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   888
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   889
    /**
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   890
     * Creates a lazy concatenated {@code Stream} whose elements are all the
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   891
     * elements of a first {@code Stream} succeeded by all the elements of the
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   892
     * second {@code Stream}. The resulting stream is ordered if both
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   893
     * of the input streams are ordered, and parallel if either of the input
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   894
     * streams is parallel.
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   895
     *
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   896
     * @param <T> The type of stream elements
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   897
     * @param a the first stream
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   898
     * @param b the second stream to concatenate on to end of the first
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   899
     *        stream
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   900
     * @return the concatenation of the two input streams
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   901
     */
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   902
    public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   903
        Objects.requireNonNull(a);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   904
        Objects.requireNonNull(b);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   905
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   906
        @SuppressWarnings("unchecked")
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   907
        Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   908
                (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   909
        return StreamSupport.stream(split, a.isParallel() || b.isParallel());
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   910
    }
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   911
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   912
    /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   913
     * 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
   914
     * {@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
   915
     * {@code Builder} (without the copying overhead that comes from using
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   916
     * an {@code ArrayList} as a temporary buffer.)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   917
     *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   918
     * <p>A {@code Stream.Builder} has a lifecycle, where it starts in a building
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   919
     * 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
   920
     * 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
   921
     * 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
   922
     * {@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
   923
     * builder, in the order they were added.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   924
     *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   925
     * @param <T> the type of stream elements
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   926
     * @see Stream#builder()
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   927
     * @since 1.8
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   928
     */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   929
    public interface Builder<T> extends Consumer<T> {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   930
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   931
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   932
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   933
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   934
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   935
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   936
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   937
        @Override
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   938
        void accept(T t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   939
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   940
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   941
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   942
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   943
         * @implSpec
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   944
         * The default implementation behaves as if:
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   945
         * <pre>{@code
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   946
         *     accept(t)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   947
         *     return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   948
         * }</pre>
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   949
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   950
         * @param t the element to add
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   951
         * @return {@code this} builder
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   952
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   953
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   954
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   955
        default Builder<T> add(T t) {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   956
            accept(t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   957
            return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   958
        }
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   959
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   960
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   961
         * Builds the stream, transitioning this builder to the built state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   962
         * An {@code IllegalStateException} is thrown if there are further attempts
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   963
         * 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
   964
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   965
         * @return the built stream
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   966
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   967
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   968
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   969
        Stream<T> build();
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   970
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   971
    }
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   972
}