jdk/src/share/classes/java/util/stream/Stream.java
author briangoetz
Wed, 17 Apr 2013 14:39:04 -0400
changeset 17167 87067e3340d3
child 17195 e897ad52979e
permissions -rw-r--r--
8008682: Inital Streams public API Reviewed-by: mduigou, dholmes, darcy Contributed-by: Brian Goetz <brian.goetz@oracle.com>, Mike Duigou <mike.duigou@oracle.com>, Paul Sandoz <paul.sandoz@oracle.com>, JSR-335 EG <lambda-libs-spec-experts@openjdk.java.net>
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
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    27
import java.util.Comparator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    28
import java.util.Optional;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    29
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    30
import java.util.function.BiFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    31
import java.util.function.BinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.function.Consumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    33
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    34
import java.util.function.IntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
import java.util.function.Predicate;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    36
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
import java.util.function.ToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    38
import java.util.function.ToIntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    39
import java.util.function.ToLongFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    41
// @@@ Specification to-do list @@@
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
// - Describe the difference between sequential and parallel streams
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
// - More general information about reduce, better definitions for associativity, more description of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
//   how reduce employs parallelism, more examples
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    45
// - Role of stream flags in various operations, specifically ordering
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    46
//   - Whether each op preserves encounter order
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
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    49
/**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    50
 * A sequence of elements supporting sequential and parallel bulk operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    51
 * Streams support lazy intermediate operations (transforming a stream to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    52
 * another stream) such as {@code filter} and {@code map}, and terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    53
 * operations (consuming the contents of a stream to produce a result or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    54
 * side-effect), such as {@code forEach}, {@code findFirst}, and {@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    55
 * iterator}.  Once an operation has been performed on a stream, it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    56
 * is considered <em>consumed</em> and no longer usable for other operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    57
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    58
 * <p>For sequential stream pipelines, all operations are performed in the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    59
 * <a href="package-summary.html#Ordering">encounter order</a> of the pipeline
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    60
 * source, if the pipeline source has a defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    61
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    62
 * <p>For parallel stream pipelines, unless otherwise specified, intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    63
 * stream operations preserve the <a href="package-summary.html#Ordering">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    64
 * encounter order</a> of their source, and terminal operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    65
 * respect the encounter order of their source, if the source
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    66
 * has an encounter order.  Provided that and parameters to stream operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    67
 * satisfy the <a href="package-summary.html#NonInterference">non-interference
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    68
 * requirements</a>, and excepting differences arising from the absence of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    69
 * a defined encounter order, the result of a stream pipeline should be the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    70
 * stable across multiple executions of the same operations on the same source.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    71
 * However, the timing and thread in which side-effects occur (for those
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    72
 * operations which are allowed to produce side-effects, such as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    73
 * {@link #forEach(Consumer)}), are explicitly nondeterministic for parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    74
 * execution of stream pipelines.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    75
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    76
 * <p>Unless otherwise noted, passing a {@code null} argument to any stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    77
 * method may result in a {@link NullPointerException}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    78
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    79
 * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    80
 * Streams are not data structures; they do not manage the storage for their
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    81
 * elements, nor do they support access to individual elements.  However,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    82
 * you can use the {@link #iterator()} or {@link #spliterator()} operations to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    83
 * perform a controlled traversal.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    84
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    85
 * @param <T> type of elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    86
 * @since 1.8
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    87
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    88
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    89
public interface Stream<T> extends BaseStream<T, Stream<T>> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    90
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    91
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    92
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    93
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    94
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    95
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    96
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    97
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    98
     * @param predicate a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    99
     *                  non-interfering, stateless</a> predicate to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   100
     *                  each element to determine if it should be included
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   101
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   102
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   103
    Stream<T> filter(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   104
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   105
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   106
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   107
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   108
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   110
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   111
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   112
     * @param <R> The element type of the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   113
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   114
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   115
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   116
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   118
    <R> Stream<R> map(Function<? super T, ? extends R> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   119
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   120
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   121
     * Returns an {@code IntStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   122
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   123
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   124
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   125
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   126
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   127
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   128
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   129
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   130
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   131
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   132
    IntStream mapToInt(ToIntFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   133
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   134
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   135
     * Returns a {@code LongStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   137
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   138
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   140
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   141
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   142
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   143
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   144
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   145
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   146
    LongStream mapToLong(ToLongFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   147
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   148
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   149
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   150
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   151
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   152
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   153
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   154
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   155
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   156
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   158
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   159
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
    DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   161
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   163
     * Returns a stream consisting of the results of replacing each element of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   164
     * this stream with the contents of the stream produced by applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
     * provided mapping function to each element.  If the result of the mapping
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
     * function is {@code null}, this is treated as if the result is an empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
     * stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * The {@code flatMap()} operation has the effect of applying a one-to-many
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   174
     * tranformation to the elements of the stream, and then flattening the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   175
     * resulting elements into a new stream. For example, if {@code orders}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   176
     * is a stream of purchase orders, and each purchase order contains a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   177
     * collection of line items, then the following produces a stream of line
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   178
     * items:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   179
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   180
     *     orderStream.flatMap(order -> order.getLineItems().stream())...
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   181
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
     * @param <R> The element type of the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
     *               element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
    <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
     * Returns an {@code IntStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   193
     * element of this stream with the contents of the stream produced by
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   194
     * applying the provided mapping function to each element.  If the result of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   195
     * the mapping function is {@code null}, this is treated as if the result is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   196
     * an empty stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   197
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   198
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   201
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   203
     *               element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   204
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   205
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   206
    IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   207
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   208
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   209
     * Returns a {@code LongStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   210
     * element of this stream with the contents of the stream produced
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
     * by applying the provided mapping function to each element.  If the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   212
     * of the mapping function is {@code null}, this is treated as if the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   213
     * result is an empty stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   214
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   215
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   216
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   217
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   219
     *               non-interfering, stateless</a> function to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     *               each element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   221
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   222
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
    LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
     * Returns a {@code DoubleStream} consisting of the results of replacing each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
     * element of this stream with the contents of the stream produced
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
     * by applying the provided mapping function to each element.  If the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
     * of the mapping function is {@code null}, this is treated as if the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
     * is an empty stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   231
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   232
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   233
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   234
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   235
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   236
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   237
     *               element which produces a stream of new values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   240
    DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   241
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   242
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   243
     * Returns a stream consisting of the distinct elements (according to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   244
     * {@link Object#equals(Object)}) of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   245
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   246
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   247
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   248
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   249
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
    Stream<T> distinct();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   252
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   253
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   254
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   255
     * according to natural order.  If the elements of this stream are not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   256
     * {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   257
     * when the stream pipeline is executed.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   258
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   259
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   260
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   261
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   262
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   263
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   264
    Stream<T> sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   265
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   266
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   267
     * Returns a stream consisting of the elements of this stream, sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   268
     * according to the provided {@code Comparator}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   269
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   270
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   271
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   272
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   273
     * @param comparator a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   274
     *                   non-interfering, stateless</a> {@code Comparator} to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   275
     *                   be used to compare stream elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   276
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   277
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   278
    Stream<T> sorted(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   279
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   280
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   281
     * Returns a stream consisting of the elements of this stream, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   282
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   283
     * from the resulting stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   284
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   285
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   286
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   287
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   288
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   289
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   290
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   291
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   292
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   293
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   294
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   295
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   296
     *     list.stream()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   297
     *         .filter(filteringFunction)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   298
     *         .peek(e -> {System.out.println("Filtered value: " + e); });
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   299
     *         .map(mappingFunction)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   300
     *         .peek(e -> {System.out.println("Mapped value: " + e); });
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   301
     *         .collect(Collectors.intoList());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   302
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   303
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   304
     * @param consumer a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   305
     *                 non-interfering</a> action to perform on the elements as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   306
     *                 they are consumed from the stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   307
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   308
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   309
    Stream<T> peek(Consumer<? super T> consumer);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   310
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   311
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   312
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   313
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   314
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   315
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   316
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   317
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   318
     * @param maxSize the number of elements the stream should be limited to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   319
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   320
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   321
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   322
    Stream<T> limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   323
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   324
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
     * Returns a stream consisting of the remaining elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   326
     * after indexing {@code startInclusive} elements into the stream. If the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   327
     * {@code startInclusive} index lies past the end of this stream then an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   328
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   329
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   330
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   331
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   332
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   333
     * @param startInclusive the number of leading elements to skip
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   334
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   335
     * @throws IllegalArgumentException if {@code startInclusive} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   336
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   337
    Stream<T> substream(long startInclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   338
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   339
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   340
     * Returns a stream consisting of the remaining elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   341
     * after indexing {@code startInclusive} elements into the stream and
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   342
     * truncated to contain no more than {@code endExclusive - startInclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   343
     * elements. If the {@code startInclusive} index lies past the end
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   344
     * of this stream then an empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   345
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   346
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   347
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   348
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   349
     * @param startInclusive the starting position of the substream, inclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   350
     * @param endExclusive the ending position of the substream, exclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   351
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   352
     * @throws IllegalArgumentException if {@code startInclusive} or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   353
     * {@code endExclusive} is negative or {@code startInclusive} is greater
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   354
     * than {@code endExclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   355
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   356
    Stream<T> substream(long startInclusive, long endExclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   357
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   358
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   359
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   360
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   361
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   362
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   363
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   364
     * <p>For parallel stream pipelines, this operation does <em>not</em>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   365
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   366
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   367
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   368
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   369
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   370
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   371
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   372
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   373
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   374
    void forEach(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   375
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   376
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   377
     * Performs an action for each element of this stream, guaranteeing that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   378
     * each element is processed in encounter order for streams that have a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   379
     * defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   380
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   381
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   382
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   383
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   384
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   385
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   386
     * @see #forEach(Consumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   387
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   388
    void forEachOrdered(Consumer<? super T> action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   389
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   390
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   391
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   392
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   393
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   394
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   395
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   396
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   397
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   398
    Object[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   399
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   400
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   401
     * Returns an array containing the elements of this stream, using the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   402
     * provided {@code generator} function to allocate the returned array.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   403
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   404
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   405
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   406
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   407
     * @param <A> the element type of the resulting array
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   408
     * @param generator a function which produces a new array of the desired
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   409
     *                  type and the provided length
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   410
     * @return an array containing the elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   411
     * @throws ArrayStoreException if the runtime type of the array returned
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     * from the array generator is not a supertype of the runtime type of every
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     * element in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   414
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
    <A> A[] toArray(IntFunction<A[]> generator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   419
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   423
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   424
     *     T result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   425
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   426
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   427
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   428
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   429
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   430
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   431
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   432
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   433
     * function. This means that for all {@code t},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   434
     * {@code accumulator.apply(identity, t)} is equal to {@code t}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   435
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   436
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   437
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   438
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   439
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   440
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   441
     * @apiNote Sum, min, max, average, and string concatenation are all special
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
     * cases of reduction. Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     *     Integer sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
     * or more compactly:
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, Integer::sum);
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
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   455
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   456
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   457
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   458
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   459
     * @param identity the identity value for the accumulating function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   460
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   461
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   462
     *                    stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   463
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   464
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   465
    T reduce(T identity, BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   466
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   467
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   468
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   469
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   470
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   471
     * function, and returns an {@code Optional} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   472
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   473
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   475
     *     T result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
     *     for (T element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   479
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   480
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   481
     *         else
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   482
     *             result = accumulator.apply(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   483
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   484
     *     return foundAny ? Optional.of(result) : Optional.empty();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   485
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   486
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   487
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   488
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   489
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   490
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   491
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   492
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   493
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   495
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   496
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   497
     *                    stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   498
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   499
     * @see #reduce(Object, BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   500
     * @see #min(java.util.Comparator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   501
     * @see #max(java.util.Comparator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   502
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   503
    Optional<T> reduce(BinaryOperator<T> accumulator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   504
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   505
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   506
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   507
     * elements of this stream, using the provided identity, accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   508
     * function, and a combining functions.  This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   509
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   510
     *     U result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   511
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   512
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   513
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   514
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   515
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   516
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   517
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   518
     * <p>The {@code identity} value must be an identity for the combiner
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   519
     * function.  This means that for all {@code u}, {@code combiner(identity, u)}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   520
     * is equal to {@code u}.  Additionally, the {@code combiner} function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   521
     * must be compatible with the {@code accumulator} function; for all
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   522
     * {@code u} and {@code t}, the following must hold:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   523
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   524
     *     combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   525
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   526
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   527
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   528
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   529
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   530
     * @apiNote Many reductions using this form can be represented more simply
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   531
     * by an explicit combination of {@code map} and {@code reduce} operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   532
     * The {@code accumulator} function acts as a fused mapper and accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   533
     * which can sometimes be more efficient than separate mapping and reduction,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   534
     * such as in the case where knowing the previously reduced value allows you
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   535
     * to avoid some computation.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   536
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   537
     * @param <U> The type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   538
     * @param identity the identity value for the combiner function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   539
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   540
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   541
     *                    stateless</a> function for incorporating an additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   542
     *                    element into a result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   543
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   544
     *                 <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   545
     *                 stateless</a> function for combining two values, which
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   546
     *                 must be compatible with the accumulator function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   547
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   548
     * @see #reduce(BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   549
     * @see #reduce(Object, BinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   550
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   551
    <U> U reduce(U identity,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   552
                 BiFunction<U, ? super T, U> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   553
                 BinaryOperator<U> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   554
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   555
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   556
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   557
     * reduction</a> operation on the elements of this stream.  A mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   558
     * reduction is one in which the reduced value is a mutable value holder,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   559
     * such as an {@code ArrayList}, and elements are incorporated by updating
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   560
     * the state of the result, rather than by replacing the result.  This
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   561
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   562
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   563
     *     R result = resultFactory.get();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   564
     *     for (T element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   565
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   566
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   567
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   568
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   569
     * <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   570
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   571
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   572
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   573
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   574
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   575
     * @apiNote There are many existing classes in the JDK whose signatures are
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   576
     * a good match for use as arguments to {@code collect()}.  For example,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   577
     * the following will accumulate strings into an ArrayList:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   578
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   579
     *     List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   580
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   581
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   582
     * <p>The following will take a stream of strings and concatenates them into a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   583
     * single string:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   584
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   585
     *     String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   586
     *                                          StringBuilder::append)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   587
     *                                 .toString();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   588
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   589
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   590
     * @param <R> type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   591
     * @param resultFactory a function that creates a new result container.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   592
     *                      For a parallel execution, this function may be
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   593
     *                      called multiple times and must return a fresh value
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   594
     *                      each time.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   595
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   596
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   597
     *                    stateless</a> function for incorporating an additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   598
     *                    element into a result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   599
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   600
     *                 <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   601
     *                 stateless</a> function for combining two values, which
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   602
     *                 must be compatible with the accumulator function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   603
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   604
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   605
    <R> R collect(Supplier<R> resultFactory,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   606
                  BiConsumer<R, ? super T> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   607
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   608
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   609
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   610
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   611
     * reduction</a> operation on the elements of this stream using a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   612
     * {@code Collector} object to describe the reduction.  A {@code Collector}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   613
     * encapsulates the functions used as arguments to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   614
     * {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   615
     * collection strategies, and composition of collect operations such as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   616
     * multiple-level grouping or partitioning.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   617
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   618
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   619
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   620
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   621
     * <p>When executed in parallel, multiple intermediate results may be
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   622
     * instantiated, populated, and merged, so as to maintain isolation of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   623
     * mutable data structures.  Therefore, even when executed in parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   624
     * with non-thread-safe data structures (such as {@code ArrayList}), no
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   625
     * additional synchronization is needed for a parallel reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   626
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   627
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   628
     * The following will accumulate strings into an ArrayList:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   629
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   630
     *     List<String> asList = stringStream.collect(Collectors.toList());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   633
     * <p>The following will classify {@code Person} objects by city:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   634
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   635
     *     Map<String, Collection<Person>> peopleByCity
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   636
     *         = personStream.collect(Collectors.groupBy(Person::getCity));
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 state and city,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   640
     * cascading two {@code Collector}s together:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   641
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
     *     Map<String, Map<String, Collection<Person>>> peopleByStateAndCity
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   643
     *         = personStream.collect(Collectors.groupBy(Person::getState,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   644
     *                                                   Collectors.groupBy(Person::getCity)));
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   645
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   646
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   647
     * @param <R> the type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   648
     * @param collector the {@code Collector} describing the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   649
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   650
     * @see #collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   651
     * @see Collectors
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   652
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   653
    <R> R collect(Collector<? super T, R> collector);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   654
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   655
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   656
     * Returns the minimum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   657
     * {@code Comparator}.  This is a special case of a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   658
     * <a href="package-summary.html#MutableReduction">reduction</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   659
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   660
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   661
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   662
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   663
     *                   stateless</a> {@code Comparator} to use to compare
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   664
     *                   elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   665
     * @return an {@code Optional} describing the minimum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   666
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   667
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   668
    Optional<T> min(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   669
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   670
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   671
     * Returns the maximum element of this stream according to the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   672
     * {@code Comparator}.  This is a special case of a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   673
     * <a href="package-summary.html#MutableReduction">reduction</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   674
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   675
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   676
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   677
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   678
     * @param comparator a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   679
     *                   stateless</a> {@code Comparator} to use to compare
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   680
     *                   elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   681
     * @return an {@code Optional} describing the maximum element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   682
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   683
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   684
    Optional<T> max(Comparator<? super T> comparator);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   685
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   686
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   687
     * Returns the count of elements in this stream.  This is a special case of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   688
     * a <a href="package-summary.html#MutableReduction">reduction</a> and is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   689
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   690
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   691
     *     return mapToLong(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   692
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   693
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   694
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   695
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   696
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   697
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   698
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   699
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   700
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   701
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   702
     * predicate.  May not evaluate the predicate on all elements if not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   703
     * necessary for determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   704
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   705
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   706
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   707
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   708
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   709
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   710
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   711
     * @return {@code true} if any elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   712
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   713
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   714
    boolean anyMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   715
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   716
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   717
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   718
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   719
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   720
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   721
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   722
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   723
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   724
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   725
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   726
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   727
     * @return {@code true} if all elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   728
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   729
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   730
    boolean allMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   731
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   732
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   733
     * Returns whether no elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   734
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   735
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   736
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   737
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   738
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   739
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   740
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   741
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   742
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   743
     * @return {@code true} if no elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   744
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   745
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   746
    boolean noneMatch(Predicate<? super T> predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   747
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   748
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   749
     * Returns an {@link Optional} describing the first element of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   750
     * (in the encounter order), or an empty {@code Optional} if the stream is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   751
     * empty.  If the stream has no encounter order, than any element may be
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   752
     * returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   753
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   754
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   755
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   756
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   757
     * @return an {@code Optional} describing the first element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   758
     * or an empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   759
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   760
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   761
    Optional<T> findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   762
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   763
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   764
     * Returns an {@link Optional} describing some element of the stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   765
     * empty {@code Optional} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   766
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   767
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   768
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   769
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   770
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   771
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   772
     * performance in parallel operations; the cost is that multiple invocations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   773
     * on the same source may not return the same result.  (If the first element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   774
     * in the encounter order is desired, use {@link #findFirst()} instead.)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   775
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   776
     * @return an {@code Optional} describing some element of this stream, or an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   777
     * empty {@code Optional} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   778
     * @throws NullPointerException if the element selected is null
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   779
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   780
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   781
    Optional<T> findAny();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   782
}