1 /*
2  * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package java.util.stream;
26 
27 import java.util.Arrays;
28 import java.util.LongSummaryStatistics;
29 import java.util.Objects;
30 import java.util.OptionalDouble;
31 import java.util.OptionalLong;
32 import java.util.PrimitiveIterator;
33 import java.util.Spliterator;
34 import java.util.Spliterators;
35 import java.util.function.BiConsumer;
36 import java.util.function.Function;
37 import java.util.function.LongBinaryOperator;
38 import java.util.function.LongConsumer;
39 import java.util.function.LongFunction;
40 import java.util.function.LongPredicate;
41 import java.util.function.LongSupplier;
42 import java.util.function.LongToDoubleFunction;
43 import java.util.function.LongToIntFunction;
44 import java.util.function.LongUnaryOperator;
45 import java.util.function.ObjLongConsumer;
46 import java.util.function.Supplier;
47 
48 /**
49  * A sequence of primitive long-valued elements supporting sequential and parallel
50  * aggregate operations.  This is the {@code long} primitive specialization of
51  * {@link Stream}.
52  *
53  * <p>The following example illustrates an aggregate operation using
54  * {@link Stream} and {@link LongStream}, computing the sum of the weights of the
55  * red widgets:
56  *
57  * <pre>{@code
58  *     long sum = widgets.stream()
59  *                       .filter(w -> w.getColor() == RED)
60  *                       .mapToLong(w -> w.getWeight())
61  *                       .sum();
62  * }</pre>
63  *
64  * See the class documentation for {@link Stream} and the package documentation
65  * for <a href="package-summary.html">java.util.stream</a> for additional
66  * specification of streams, stream operations, stream pipelines, and
67  * parallelism.
68  *
69  * @since 1.8
70  * @see Stream
71  * @see <a href="package-summary.html">java.util.stream</a>
72  */
73 public interface LongStream extends BaseStream<Long, LongStream> {
74 
75     /**
76      * Returns a stream consisting of the elements of this stream that match
77      * the given predicate.
78      *
79      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
80      * operation</a>.
81      *
82      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
83      *                  <a href="package-summary.html#Statelessness">stateless</a>
84      *                  predicate to apply to each element to determine if it
85      *                  should be included
86      * @return the new stream
87      */
filter(LongPredicate predicate)88     LongStream filter(LongPredicate predicate);
89 
90     /**
91      * Returns a stream consisting of the results of applying the given
92      * function to the elements of this stream.
93      *
94      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
95      * operation</a>.
96      *
97      * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
98      *               <a href="package-summary.html#Statelessness">stateless</a>
99      *               function to apply to each element
100      * @return the new stream
101      */
map(LongUnaryOperator mapper)102     LongStream map(LongUnaryOperator mapper);
103 
104     /**
105      * Returns an object-valued {@code Stream} consisting of the results of
106      * applying the given function to the elements of this stream.
107      *
108      * <p>This is an <a href="package-summary.html#StreamOps">
109      *     intermediate operation</a>.
110      *
111      * @param <U> the element type of the new stream
112      * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
113      *               <a href="package-summary.html#Statelessness">stateless</a>
114      *               function to apply to each element
115      * @return the new stream
116      */
mapToObj(LongFunction<? extends U> mapper)117     <U> Stream<U> mapToObj(LongFunction<? extends U> mapper);
118 
119     /**
120      * Returns an {@code IntStream} consisting of the results of applying the
121      * given function to the elements of this stream.
122      *
123      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
124      * operation</a>.
125      *
126      * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
127      *               <a href="package-summary.html#Statelessness">stateless</a>
128      *               function to apply to each element
129      * @return the new stream
130      */
mapToInt(LongToIntFunction mapper)131     IntStream mapToInt(LongToIntFunction mapper);
132 
133     /**
134      * Returns a {@code DoubleStream} consisting of the results of applying the
135      * given function to the elements of this stream.
136      *
137      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
138      * operation</a>.
139      *
140      * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
141      *               <a href="package-summary.html#Statelessness">stateless</a>
142      *               function to apply to each element
143      * @return the new stream
144      */
mapToDouble(LongToDoubleFunction mapper)145     DoubleStream mapToDouble(LongToDoubleFunction mapper);
146 
147     /**
148      * Returns a stream consisting of the results of replacing each element of
149      * this stream with the contents of a mapped stream produced by applying
150      * the provided mapping function to each element.  Each mapped stream is
151      * {@link java.util.stream.BaseStream#close() closed} after its contents
152      * have been placed into this stream.  (If a mapped stream is {@code null}
153      * an empty stream is used, instead.)
154      *
155      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
156      * operation</a>.
157      *
158      * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
159      *               <a href="package-summary.html#Statelessness">stateless</a>
160      *               function to apply to each element which produces a
161      *               {@code LongStream} of new values
162      * @return the new stream
163      * @see Stream#flatMap(Function)
164      */
flatMap(LongFunction<? extends LongStream> mapper)165     LongStream flatMap(LongFunction<? extends LongStream> mapper);
166 
167     /**
168      * Returns a stream consisting of the results of replacing each element of
169      * this stream with multiple elements, specifically zero or more elements.
170      * Replacement is performed by applying the provided mapping function to each
171      * element in conjunction with a {@linkplain LongConsumer consumer} argument
172      * that accepts replacement elements. The mapping function calls the consumer
173      * zero or more times to provide the replacement elements.
174      *
175      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
176      * operation</a>.
177      *
178      * <p>If the {@linkplain LongConsumer consumer} argument is used outside the scope of
179      * its application to the mapping function, the results are undefined.
180      *
181      * @implSpec
182      * The default implementation invokes {@link #flatMap flatMap} on this stream,
183      * passing a function that behaves as follows. First, it calls the mapper function
184      * with a {@code LongConsumer} that accumulates replacement elements into a newly created
185      * internal buffer. When the mapper function returns, it creates a {@code LongStream} from the
186      * internal buffer. Finally, it returns this stream to {@code flatMap}.
187      *
188      * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
189      *               <a href="package-summary.html#Statelessness">stateless</a>
190      *               function that generates replacement elements
191      * @return the new stream
192      * @see Stream#mapMulti Stream.mapMulti
193      * @since 16
194      */
mapMulti(LongMapMultiConsumer mapper)195     default LongStream mapMulti(LongMapMultiConsumer mapper) {
196         Objects.requireNonNull(mapper);
197         return flatMap(e -> {
198             SpinedBuffer.OfLong buffer = new SpinedBuffer.OfLong();
199             mapper.accept(e, buffer);
200             return StreamSupport.longStream(buffer.spliterator(), false);
201         });
202     }
203 
204     /**
205      * Returns a stream consisting of the distinct elements of this stream.
206      *
207      * <p>This is a <a href="package-summary.html#StreamOps">stateful
208      * intermediate operation</a>.
209      *
210      * @return the new stream
211      */
distinct()212     LongStream distinct();
213 
214     /**
215      * Returns a stream consisting of the elements of this stream in sorted
216      * order.
217      *
218      * <p>This is a <a href="package-summary.html#StreamOps">stateful
219      * intermediate operation</a>.
220      *
221      * @return the new stream
222      */
sorted()223     LongStream sorted();
224 
225     /**
226      * Returns a stream consisting of the elements of this stream, additionally
227      * performing the provided action on each element as elements are consumed
228      * from the resulting stream.
229      *
230      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
231      * operation</a>.
232      *
233      * <p>For parallel stream pipelines, the action may be called at
234      * whatever time and in whatever thread the element is made available by the
235      * upstream operation.  If the action modifies shared state,
236      * it is responsible for providing the required synchronization.
237      *
238      * @apiNote This method exists mainly to support debugging, where you want
239      * to see the elements as they flow past a certain point in a pipeline:
240      * <pre>{@code
241      *     LongStream.of(1, 2, 3, 4)
242      *         .filter(e -> e > 2)
243      *         .peek(e -> System.out.println("Filtered value: " + e))
244      *         .map(e -> e * e)
245      *         .peek(e -> System.out.println("Mapped value: " + e))
246      *         .sum();
247      * }</pre>
248      *
249      * <p>In cases where the stream implementation is able to optimize away the
250      * production of some or all the elements (such as with short-circuiting
251      * operations like {@code findFirst}, or in the example described in
252      * {@link #count}), the action will not be invoked for those elements.
253      *
254      * @param action a <a href="package-summary.html#NonInterference">
255      *               non-interfering</a> action to perform on the elements as
256      *               they are consumed from the stream
257      * @return the new stream
258      */
peek(LongConsumer action)259     LongStream peek(LongConsumer action);
260 
261     /**
262      * Returns a stream consisting of the elements of this stream, truncated
263      * to be no longer than {@code maxSize} in length.
264      *
265      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
266      * stateful intermediate operation</a>.
267      *
268      * @apiNote
269      * While {@code limit()} is generally a cheap operation on sequential
270      * stream pipelines, it can be quite expensive on ordered parallel pipelines,
271      * especially for large values of {@code maxSize}, since {@code limit(n)}
272      * is constrained to return not just any <em>n</em> elements, but the
273      * <em>first n</em> elements in the encounter order.  Using an unordered
274      * stream source (such as {@link #generate(LongSupplier)}) or removing the
275      * ordering constraint with {@link #unordered()} may result in significant
276      * speedups of {@code limit()} in parallel pipelines, if the semantics of
277      * your situation permit.  If consistency with encounter order is required,
278      * and you are experiencing poor performance or memory utilization with
279      * {@code limit()} in parallel pipelines, switching to sequential execution
280      * with {@link #sequential()} may improve performance.
281      *
282      * @param maxSize the number of elements the stream should be limited to
283      * @return the new stream
284      * @throws IllegalArgumentException if {@code maxSize} is negative
285      */
limit(long maxSize)286     LongStream limit(long maxSize);
287 
288     /**
289      * Returns a stream consisting of the remaining elements of this stream
290      * after discarding the first {@code n} elements of the stream.
291      * If this stream contains fewer than {@code n} elements then an
292      * empty stream will be returned.
293      *
294      * <p>This is a <a href="package-summary.html#StreamOps">stateful
295      * intermediate operation</a>.
296      *
297      * @apiNote
298      * While {@code skip()} is generally a cheap operation on sequential
299      * stream pipelines, it can be quite expensive on ordered parallel pipelines,
300      * especially for large values of {@code n}, since {@code skip(n)}
301      * is constrained to skip not just any <em>n</em> elements, but the
302      * <em>first n</em> elements in the encounter order.  Using an unordered
303      * stream source (such as {@link #generate(LongSupplier)}) or removing the
304      * ordering constraint with {@link #unordered()} may result in significant
305      * speedups of {@code skip()} in parallel pipelines, if the semantics of
306      * your situation permit.  If consistency with encounter order is required,
307      * and you are experiencing poor performance or memory utilization with
308      * {@code skip()} in parallel pipelines, switching to sequential execution
309      * with {@link #sequential()} may improve performance.
310      *
311      * @param n the number of leading elements to skip
312      * @return the new stream
313      * @throws IllegalArgumentException if {@code n} is negative
314      */
skip(long n)315     LongStream skip(long n);
316 
317     /**
318      * Returns, if this stream is ordered, a stream consisting of the longest
319      * prefix of elements taken from this stream that match the given predicate.
320      * Otherwise returns, if this stream is unordered, a stream consisting of a
321      * subset of elements taken from this stream that match the given predicate.
322      *
323      * <p>If this stream is ordered then the longest prefix is a contiguous
324      * sequence of elements of this stream that match the given predicate.  The
325      * first element of the sequence is the first element of this stream, and
326      * the element immediately following the last element of the sequence does
327      * not match the given predicate.
328      *
329      * <p>If this stream is unordered, and some (but not all) elements of this
330      * stream match the given predicate, then the behavior of this operation is
331      * nondeterministic; it is free to take any subset of matching elements
332      * (which includes the empty set).
333      *
334      * <p>Independent of whether this stream is ordered or unordered if all
335      * elements of this stream match the given predicate then this operation
336      * takes all elements (the result is the same as the input), or if no
337      * elements of the stream match the given predicate then no elements are
338      * taken (the result is an empty stream).
339      *
340      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
341      * stateful intermediate operation</a>.
342      *
343      * @implSpec
344      * The default implementation obtains the {@link #spliterator() spliterator}
345      * of this stream, wraps that spliterator so as to support the semantics
346      * of this operation on traversal, and returns a new stream associated with
347      * the wrapped spliterator.  The returned stream preserves the execution
348      * characteristics of this stream (namely parallel or sequential execution
349      * as per {@link #isParallel()}) but the wrapped spliterator may choose to
350      * not support splitting.  When the returned stream is closed, the close
351      * handlers for both the returned and this stream are invoked.
352      *
353      * @apiNote
354      * While {@code takeWhile()} is generally a cheap operation on sequential
355      * stream pipelines, it can be quite expensive on ordered parallel
356      * pipelines, since the operation is constrained to return not just any
357      * valid prefix, but the longest prefix of elements in the encounter order.
358      * Using an unordered stream source (such as
359      * {@link #generate(LongSupplier)}) or removing the ordering constraint with
360      * {@link #unordered()} may result in significant speedups of
361      * {@code takeWhile()} in parallel pipelines, if the semantics of your
362      * situation permit.  If consistency with encounter order is required, and
363      * you are experiencing poor performance or memory utilization with
364      * {@code takeWhile()} in parallel pipelines, switching to sequential
365      * execution with {@link #sequential()} may improve performance.
366      *
367      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
368      *                  <a href="package-summary.html#Statelessness">stateless</a>
369      *                  predicate to apply to elements to determine the longest
370      *                  prefix of elements.
371      * @return the new stream
372      * @since 9
373      */
takeWhile(LongPredicate predicate)374     default LongStream takeWhile(LongPredicate predicate) {
375         Objects.requireNonNull(predicate);
376         // Reuses the unordered spliterator, which, when encounter is present,
377         // is safe to use as long as it configured not to split
378         return StreamSupport.longStream(
379                 new WhileOps.UnorderedWhileSpliterator.OfLong.Taking(spliterator(), true, predicate),
380                 isParallel()).onClose(this::close);
381     }
382 
383     /**
384      * Returns, if this stream is ordered, a stream consisting of the remaining
385      * elements of this stream after dropping the longest prefix of elements
386      * that match the given predicate.  Otherwise returns, if this stream is
387      * unordered, a stream consisting of the remaining elements of this stream
388      * after dropping a subset of elements that match the given predicate.
389      *
390      * <p>If this stream is ordered then the longest prefix is a contiguous
391      * sequence of elements of this stream that match the given predicate.  The
392      * first element of the sequence is the first element of this stream, and
393      * the element immediately following the last element of the sequence does
394      * not match the given predicate.
395      *
396      * <p>If this stream is unordered, and some (but not all) elements of this
397      * stream match the given predicate, then the behavior of this operation is
398      * nondeterministic; it is free to drop any subset of matching elements
399      * (which includes the empty set).
400      *
401      * <p>Independent of whether this stream is ordered or unordered if all
402      * elements of this stream match the given predicate then this operation
403      * drops all elements (the result is an empty stream), or if no elements of
404      * the stream match the given predicate then no elements are dropped (the
405      * result is the same as the input).
406      *
407      * <p>This is a <a href="package-summary.html#StreamOps">stateful
408      * intermediate operation</a>.
409      *
410      * @implSpec
411      * The default implementation obtains the {@link #spliterator() spliterator}
412      * of this stream, wraps that spliterator so as to support the semantics
413      * of this operation on traversal, and returns a new stream associated with
414      * the wrapped spliterator.  The returned stream preserves the execution
415      * characteristics of this stream (namely parallel or sequential execution
416      * as per {@link #isParallel()}) but the wrapped spliterator may choose to
417      * not support splitting.  When the returned stream is closed, the close
418      * handlers for both the returned and this stream are invoked.
419      *
420      * @apiNote
421      * While {@code dropWhile()} is generally a cheap operation on sequential
422      * stream pipelines, it can be quite expensive on ordered parallel
423      * pipelines, since the operation is constrained to return not just any
424      * valid prefix, but the longest prefix of elements in the encounter order.
425      * Using an unordered stream source (such as
426      * {@link #generate(LongSupplier)}) or removing the ordering constraint with
427      * {@link #unordered()} may result in significant speedups of
428      * {@code dropWhile()} in parallel pipelines, if the semantics of your
429      * situation permit.  If consistency with encounter order is required, and
430      * you are experiencing poor performance or memory utilization with
431      * {@code dropWhile()} in parallel pipelines, switching to sequential
432      * execution with {@link #sequential()} may improve performance.
433      *
434      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
435      *                  <a href="package-summary.html#Statelessness">stateless</a>
436      *                  predicate to apply to elements to determine the longest
437      *                  prefix of elements.
438      * @return the new stream
439      * @since 9
440      */
dropWhile(LongPredicate predicate)441     default LongStream dropWhile(LongPredicate predicate) {
442         Objects.requireNonNull(predicate);
443         // Reuses the unordered spliterator, which, when encounter is present,
444         // is safe to use as long as it configured not to split
445         return StreamSupport.longStream(
446                 new WhileOps.UnorderedWhileSpliterator.OfLong.Dropping(spliterator(), true, predicate),
447                 isParallel()).onClose(this::close);
448     }
449 
450     /**
451      * Performs an action for each element of this stream.
452      *
453      * <p>This is a <a href="package-summary.html#StreamOps">terminal
454      * operation</a>.
455      *
456      * <p>For parallel stream pipelines, this operation does <em>not</em>
457      * guarantee to respect the encounter order of the stream, as doing so
458      * would sacrifice the benefit of parallelism.  For any given element, the
459      * action may be performed at whatever time and in whatever thread the
460      * library chooses.  If the action accesses shared state, it is
461      * responsible for providing the required synchronization.
462      *
463      * @param action a <a href="package-summary.html#NonInterference">
464      *               non-interfering</a> action to perform on the elements
465      */
forEach(LongConsumer action)466     void forEach(LongConsumer action);
467 
468     /**
469      * Performs an action for each element of this stream, guaranteeing that
470      * each element is processed in encounter order for streams that have a
471      * defined encounter order.
472      *
473      * <p>This is a <a href="package-summary.html#StreamOps">terminal
474      * operation</a>.
475      *
476      * @param action a <a href="package-summary.html#NonInterference">
477      *               non-interfering</a> action to perform on the elements
478      * @see #forEach(LongConsumer)
479      */
forEachOrdered(LongConsumer action)480     void forEachOrdered(LongConsumer action);
481 
482     /**
483      * Returns an array containing the elements of this stream.
484      *
485      * <p>This is a <a href="package-summary.html#StreamOps">terminal
486      * operation</a>.
487      *
488      * @return an array containing the elements of this stream
489      */
toArray()490     long[] toArray();
491 
492     /**
493      * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
494      * elements of this stream, using the provided identity value and an
495      * <a href="package-summary.html#Associativity">associative</a>
496      * accumulation function, and returns the reduced value.  This is equivalent
497      * to:
498      * <pre>{@code
499      *     long result = identity;
500      *     for (long element : this stream)
501      *         result = accumulator.applyAsLong(result, element)
502      *     return result;
503      * }</pre>
504      *
505      * but is not constrained to execute sequentially.
506      *
507      * <p>The {@code identity} value must be an identity for the accumulator
508      * function. This means that for all {@code x},
509      * {@code accumulator.apply(identity, x)} is equal to {@code x}.
510      * The {@code accumulator} function must be an
511      * <a href="package-summary.html#Associativity">associative</a> function.
512      *
513      * <p>This is a <a href="package-summary.html#StreamOps">terminal
514      * operation</a>.
515      *
516      * @apiNote Sum, min, max, and average are all special cases of reduction.
517      * Summing a stream of numbers can be expressed as:
518      *
519      * <pre>{@code
520      *     long sum = integers.reduce(0, (a, b) -> a+b);
521      * }</pre>
522      *
523      * or more compactly:
524      *
525      * <pre>{@code
526      *     long sum = integers.reduce(0, Long::sum);
527      * }</pre>
528      *
529      * <p>While this may seem a more roundabout way to perform an aggregation
530      * compared to simply mutating a running total in a loop, reduction
531      * operations parallelize more gracefully, without needing additional
532      * synchronization and with greatly reduced risk of data races.
533      *
534      * @param identity the identity value for the accumulating function
535      * @param op an <a href="package-summary.html#Associativity">associative</a>,
536      *           <a href="package-summary.html#NonInterference">non-interfering</a>,
537      *           <a href="package-summary.html#Statelessness">stateless</a>
538      *           function for combining two values
539      * @return the result of the reduction
540      * @see #sum()
541      * @see #min()
542      * @see #max()
543      * @see #average()
544      */
reduce(long identity, LongBinaryOperator op)545     long reduce(long identity, LongBinaryOperator op);
546 
547     /**
548      * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
549      * elements of this stream, using an
550      * <a href="package-summary.html#Associativity">associative</a> accumulation
551      * function, and returns an {@code OptionalLong} describing the reduced value,
552      * if any. This is equivalent to:
553      * <pre>{@code
554      *     boolean foundAny = false;
555      *     long result = null;
556      *     for (long element : this stream) {
557      *         if (!foundAny) {
558      *             foundAny = true;
559      *             result = element;
560      *         }
561      *         else
562      *             result = accumulator.applyAsLong(result, element);
563      *     }
564      *     return foundAny ? OptionalLong.of(result) : OptionalLong.empty();
565      * }</pre>
566      *
567      * but is not constrained to execute sequentially.
568      *
569      * <p>The {@code accumulator} function must be an
570      * <a href="package-summary.html#Associativity">associative</a> function.
571      *
572      * <p>This is a <a href="package-summary.html#StreamOps">terminal
573      * operation</a>.
574      *
575      * @param op an <a href="package-summary.html#Associativity">associative</a>,
576      *           <a href="package-summary.html#NonInterference">non-interfering</a>,
577      *           <a href="package-summary.html#Statelessness">stateless</a>
578      *           function for combining two values
579      * @return the result of the reduction
580      * @see #reduce(long, LongBinaryOperator)
581      */
reduce(LongBinaryOperator op)582     OptionalLong reduce(LongBinaryOperator op);
583 
584     /**
585      * Performs a <a href="package-summary.html#MutableReduction">mutable
586      * reduction</a> operation on the elements of this stream.  A mutable
587      * reduction is one in which the reduced value is a mutable result container,
588      * such as an {@code ArrayList}, and elements are incorporated by updating
589      * the state of the result rather than by replacing the result.  This
590      * produces a result equivalent to:
591      * <pre>{@code
592      *     R result = supplier.get();
593      *     for (long element : this stream)
594      *         accumulator.accept(result, element);
595      *     return result;
596      * }</pre>
597      *
598      * <p>Like {@link #reduce(long, LongBinaryOperator)}, {@code collect} operations
599      * can be parallelized without requiring additional synchronization.
600      *
601      * <p>This is a <a href="package-summary.html#StreamOps">terminal
602      * operation</a>.
603      *
604      * @param <R> the type of the mutable result container
605      * @param supplier a function that creates a new mutable result container.
606      *                 For a parallel execution, this function may be called
607      *                 multiple times and must return a fresh value each time.
608      * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
609      *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
610      *                    <a href="package-summary.html#Statelessness">stateless</a>
611      *                    function that must fold an element into a result
612      *                    container.
613      * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
614      *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
615      *                    <a href="package-summary.html#Statelessness">stateless</a>
616      *                    function that accepts two partial result containers
617      *                    and merges them, which must be compatible with the
618      *                    accumulator function.  The combiner function must fold
619      *                    the elements from the second result container into the
620      *                    first result container.
621      * @return the result of the reduction
622      * @see Stream#collect(Supplier, BiConsumer, BiConsumer)
623      */
collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner)624     <R> R collect(Supplier<R> supplier,
625                   ObjLongConsumer<R> accumulator,
626                   BiConsumer<R, R> combiner);
627 
628     /**
629      * Returns the sum of elements in this stream.  This is a special case
630      * of a <a href="package-summary.html#Reduction">reduction</a>
631      * and is equivalent to:
632      * <pre>{@code
633      *     return reduce(0, Long::sum);
634      * }</pre>
635      *
636      * <p>This is a <a href="package-summary.html#StreamOps">terminal
637      * operation</a>.
638      *
639      * @return the sum of elements in this stream
640      */
sum()641     long sum();
642 
643     /**
644      * Returns an {@code OptionalLong} describing the minimum element of this
645      * stream, or an empty optional if this stream is empty.  This is a special
646      * case of a <a href="package-summary.html#Reduction">reduction</a>
647      * and is equivalent to:
648      * <pre>{@code
649      *     return reduce(Long::min);
650      * }</pre>
651      *
652      * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
653      *
654      * @return an {@code OptionalLong} containing the minimum element of this
655      * stream, or an empty {@code OptionalLong} if the stream is empty
656      */
min()657     OptionalLong min();
658 
659     /**
660      * Returns an {@code OptionalLong} describing the maximum element of this
661      * stream, or an empty optional if this stream is empty.  This is a special
662      * case of a <a href="package-summary.html#Reduction">reduction</a>
663      * and is equivalent to:
664      * <pre>{@code
665      *     return reduce(Long::max);
666      * }</pre>
667      *
668      * <p>This is a <a href="package-summary.html#StreamOps">terminal
669      * operation</a>.
670      *
671      * @return an {@code OptionalLong} containing the maximum element of this
672      * stream, or an empty {@code OptionalLong} if the stream is empty
673      */
max()674     OptionalLong max();
675 
676     /**
677      * Returns the count of elements in this stream.  This is a special case of
678      * a <a href="package-summary.html#Reduction">reduction</a> and is
679      * equivalent to:
680      * <pre>{@code
681      *     return map(e -> 1L).sum();
682      * }</pre>
683      *
684      * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
685      *
686      * @apiNote
687      * An implementation may choose to not execute the stream pipeline (either
688      * sequentially or in parallel) if it is capable of computing the count
689      * directly from the stream source.  In such cases no source elements will
690      * be traversed and no intermediate operations will be evaluated.
691      * Behavioral parameters with side-effects, which are strongly discouraged
692      * except for harmless cases such as debugging, may be affected.  For
693      * example, consider the following stream:
694      * <pre>{@code
695      *     LongStream s = LongStream.of(1, 2, 3, 4);
696      *     long count = s.peek(System.out::println).count();
697      * }</pre>
698      * The number of elements covered by the stream source is known and the
699      * intermediate operation, {@code peek}, does not inject into or remove
700      * elements from the stream (as may be the case for {@code flatMap} or
701      * {@code filter} operations).  Thus the count is 4 and there is no need to
702      * execute the pipeline and, as a side-effect, print out the elements.
703      *
704      * @return the count of elements in this stream
705      */
count()706     long count();
707 
708     /**
709      * Returns an {@code OptionalDouble} describing the arithmetic mean of elements of
710      * this stream, or an empty optional if this stream is empty.  This is a
711      * special case of a
712      * <a href="package-summary.html#Reduction">reduction</a>.
713      *
714      * <p>This is a <a href="package-summary.html#StreamOps">terminal
715      * operation</a>.
716      *
717      * @return an {@code OptionalDouble} containing the average element of this
718      * stream, or an empty optional if the stream is empty
719      */
average()720     OptionalDouble average();
721 
722     /**
723      * Returns a {@code LongSummaryStatistics} describing various summary data
724      * about the elements of this stream.  This is a special case of a
725      * <a href="package-summary.html#Reduction">reduction</a>.
726      *
727      * <p>This is a <a href="package-summary.html#StreamOps">terminal
728      * operation</a>.
729      *
730      * @return a {@code LongSummaryStatistics} describing various summary data
731      * about the elements of this stream
732      */
summaryStatistics()733     LongSummaryStatistics summaryStatistics();
734 
735     /**
736      * Returns whether any elements of this stream match the provided
737      * predicate.  May not evaluate the predicate on all elements if not
738      * necessary for determining the result.  If the stream is empty then
739      * {@code false} is returned and the predicate is not evaluated.
740      *
741      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
742      * terminal operation</a>.
743      *
744      * @apiNote
745      * This method evaluates the <em>existential quantification</em> of the
746      * predicate over the elements of the stream (for some x P(x)).
747      *
748      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
749      *                  <a href="package-summary.html#Statelessness">stateless</a>
750      *                  predicate to apply to elements of this stream
751      * @return {@code true} if any elements of the stream match the provided
752      * predicate, otherwise {@code false}
753      */
754     boolean anyMatch(LongPredicate predicate);
755 
756     /**
757      * Returns whether all elements of this stream match the provided predicate.
758      * May not evaluate the predicate on all elements if not necessary for
759      * determining the result.  If the stream is empty then {@code true} is
760      * returned and the predicate is not evaluated.
761      *
762      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
763      * terminal operation</a>.
764      *
765      * @apiNote
766      * This method evaluates the <em>universal quantification</em> of the
767      * predicate over the elements of the stream (for all x P(x)).  If the
768      * stream is empty, the quantification is said to be <em>vacuously
769      * satisfied</em> and is always {@code true} (regardless of P(x)).
770      *
771      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
772      *                  <a href="package-summary.html#Statelessness">stateless</a>
773      *                  predicate to apply to elements of this stream
774      * @return {@code true} if either all elements of the stream match the
775      * provided predicate or the stream is empty, otherwise {@code false}
776      */
777     boolean allMatch(LongPredicate predicate);
778 
779     /**
780      * Returns whether no elements of this stream match the provided predicate.
781      * May not evaluate the predicate on all elements if not necessary for
782      * determining the result.  If the stream is empty then {@code true} is
783      * returned and the predicate is not evaluated.
784      *
785      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
786      * terminal operation</a>.
787      *
788      * @apiNote
789      * This method evaluates the <em>universal quantification</em> of the
790      * negated predicate over the elements of the stream (for all x ~P(x)).  If
791      * the stream is empty, the quantification is said to be vacuously satisfied
792      * and is always {@code true}, regardless of P(x).
793      *
794      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
795      *                  <a href="package-summary.html#Statelessness">stateless</a>
796      *                  predicate to apply to elements of this stream
797      * @return {@code true} if either no elements of the stream match the
798      * provided predicate or the stream is empty, otherwise {@code false}
799      */
800     boolean noneMatch(LongPredicate predicate);
801 
802     /**
803      * Returns an {@link OptionalLong} describing the first element of this
804      * stream, or an empty {@code OptionalLong} if the stream is empty.  If the
805      * stream has no encounter order, then any element may be returned.
806      *
807      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
808      * terminal operation</a>.
809      *
810      * @return an {@code OptionalLong} describing the first element of this
811      * stream, or an empty {@code OptionalLong} if the stream is empty
812      */
findFirst()813     OptionalLong findFirst();
814 
815     /**
816      * Returns an {@link OptionalLong} describing some element of the stream, or
817      * an empty {@code OptionalLong} if the stream is empty.
818      *
819      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
820      * terminal operation</a>.
821      *
822      * <p>The behavior of this operation is explicitly nondeterministic; it is
823      * free to select any element in the stream.  This is to allow for maximal
824      * performance in parallel operations; the cost is that multiple invocations
825      * on the same source may not return the same result.  (If a stable result
826      * is desired, use {@link #findFirst()} instead.)
827      *
828      * @return an {@code OptionalLong} describing some element of this stream,
829      * or an empty {@code OptionalLong} if the stream is empty
830      * @see #findFirst()
831      */
findAny()832     OptionalLong findAny();
833 
834     /**
835      * Returns a {@code DoubleStream} consisting of the elements of this stream,
836      * converted to {@code double}.
837      *
838      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
839      * operation</a>.
840      *
841      * @return a {@code DoubleStream} consisting of the elements of this stream,
842      * converted to {@code double}
843      */
asDoubleStream()844     DoubleStream asDoubleStream();
845 
846     /**
847      * Returns a {@code Stream} consisting of the elements of this stream,
848      * each boxed to a {@code Long}.
849      *
850      * <p>This is an <a href="package-summary.html#StreamOps">intermediate
851      * operation</a>.
852      *
853      * @return a {@code Stream} consistent of the elements of this stream,
854      * each boxed to {@code Long}
855      */
boxed()856     Stream<Long> boxed();
857 
858     @Override
sequential()859     LongStream sequential();
860 
861     @Override
parallel()862     LongStream parallel();
863 
864     @Override
iterator()865     PrimitiveIterator.OfLong iterator();
866 
867     @Override
spliterator()868     Spliterator.OfLong spliterator();
869 
870     // Static factories
871 
872     /**
873      * Returns a builder for a {@code LongStream}.
874      *
875      * @return a stream builder
876      */
builder()877     public static Builder builder() {
878         return new Streams.LongStreamBuilderImpl();
879     }
880 
881     /**
882      * Returns an empty sequential {@code LongStream}.
883      *
884      * @return an empty sequential stream
885      */
empty()886     public static LongStream empty() {
887         return StreamSupport.longStream(Spliterators.emptyLongSpliterator(), false);
888     }
889 
890     /**
891      * Returns a sequential {@code LongStream} containing a single element.
892      *
893      * @param t the single element
894      * @return a singleton sequential stream
895      */
of(long t)896     public static LongStream of(long t) {
897         return StreamSupport.longStream(new Streams.LongStreamBuilderImpl(t), false);
898     }
899 
900     /**
901      * Returns a sequential ordered stream whose elements are the specified values.
902      *
903      * @param values the elements of the new stream
904      * @return the new stream
905      */
of(long... values)906     public static LongStream of(long... values) {
907         return Arrays.stream(values);
908     }
909 
910     /**
911      * Returns an infinite sequential ordered {@code LongStream} produced by iterative
912      * application of a function {@code f} to an initial element {@code seed},
913      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
914      * {@code f(f(seed))}, etc.
915      *
916      * <p>The first element (position {@code 0}) in the {@code LongStream} will
917      * be the provided {@code seed}.  For {@code n > 0}, the element at position
918      * {@code n}, will be the result of applying the function {@code f} to the
919      * element at position {@code n - 1}.
920      *
921      * <p>The action of applying {@code f} for one element
922      * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
923      * the action of applying {@code f} for subsequent elements.  For any given
924      * element the action may be performed in whatever thread the library
925      * chooses.
926      *
927      * @param seed the initial element
928      * @param f a function to be applied to the previous element to produce
929      *          a new element
930      * @return a new sequential {@code LongStream}
931      */
iterate(final long seed, final LongUnaryOperator f)932     public static LongStream iterate(final long seed, final LongUnaryOperator f) {
933         Objects.requireNonNull(f);
934         Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
935                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
936             long prev;
937             boolean started;
938 
939             @Override
940             public boolean tryAdvance(LongConsumer action) {
941                 Objects.requireNonNull(action);
942                 long t;
943                 if (started)
944                     t = f.applyAsLong(prev);
945                 else {
946                     t = seed;
947                     started = true;
948                 }
949                 action.accept(prev = t);
950                 return true;
951             }
952         };
953         return StreamSupport.longStream(spliterator, false);
954     }
955 
956     /**
957      * Returns a sequential ordered {@code LongStream} produced by iterative
958      * application of the given {@code next} function to an initial element,
959      * conditioned on satisfying the given {@code hasNext} predicate.  The
960      * stream terminates as soon as the {@code hasNext} predicate returns false.
961      *
962      * <p>{@code LongStream.iterate} should produce the same sequence of elements as
963      * produced by the corresponding for-loop:
964      * <pre>{@code
965      *     for (long index=seed; hasNext.test(index); index = next.applyAsLong(index)) {
966      *         ...
967      *     }
968      * }</pre>
969      *
970      * <p>The resulting sequence may be empty if the {@code hasNext} predicate
971      * does not hold on the seed value.  Otherwise the first element will be the
972      * supplied {@code seed} value, the next element (if present) will be the
973      * result of applying the {@code next} function to the {@code seed} value,
974      * and so on iteratively until the {@code hasNext} predicate indicates that
975      * the stream should terminate.
976      *
977      * <p>The action of applying the {@code hasNext} predicate to an element
978      * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
979      * the action of applying the {@code next} function to that element.  The
980      * action of applying the {@code next} function for one element
981      * <i>happens-before</i> the action of applying the {@code hasNext}
982      * predicate for subsequent elements.  For any given element an action may
983      * be performed in whatever thread the library chooses.
984      *
985      * @param seed the initial element
986      * @param hasNext a predicate to apply to elements to determine when the
987      *                stream must terminate.
988      * @param next a function to be applied to the previous element to produce
989      *             a new element
990      * @return a new sequential {@code LongStream}
991      * @since 9
992      */
iterate(long seed, LongPredicate hasNext, LongUnaryOperator next)993     public static LongStream iterate(long seed, LongPredicate hasNext, LongUnaryOperator next) {
994         Objects.requireNonNull(next);
995         Objects.requireNonNull(hasNext);
996         Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
997                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
998             long prev;
999             boolean started, finished;
1000 
1001             @Override
1002             public boolean tryAdvance(LongConsumer action) {
1003                 Objects.requireNonNull(action);
1004                 if (finished)
1005                     return false;
1006                 long t;
1007                 if (started)
1008                     t = next.applyAsLong(prev);
1009                 else {
1010                     t = seed;
1011                     started = true;
1012                 }
1013                 if (!hasNext.test(t)) {
1014                     finished = true;
1015                     return false;
1016                 }
1017                 action.accept(prev = t);
1018                 return true;
1019             }
1020 
1021             @Override
1022             public void forEachRemaining(LongConsumer action) {
1023                 Objects.requireNonNull(action);
1024                 if (finished)
1025                     return;
1026                 finished = true;
1027                 long t = started ? next.applyAsLong(prev) : seed;
1028                 while (hasNext.test(t)) {
1029                     action.accept(t);
1030                     t = next.applyAsLong(t);
1031                 }
1032             }
1033         };
1034         return StreamSupport.longStream(spliterator, false);
1035     }
1036 
1037     /**
1038      * Returns an infinite sequential unordered stream where each element is
1039      * generated by the provided {@code LongSupplier}.  This is suitable for
1040      * generating constant streams, streams of random elements, etc.
1041      *
1042      * @param s the {@code LongSupplier} for generated elements
1043      * @return a new infinite sequential unordered {@code LongStream}
1044      */
generate(LongSupplier s)1045     public static LongStream generate(LongSupplier s) {
1046         Objects.requireNonNull(s);
1047         return StreamSupport.longStream(
1048                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
1049     }
1050 
1051     /**
1052      * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
1053      * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
1054      * {@code 1}.
1055      *
1056      * @apiNote
1057      * <p>An equivalent sequence of increasing values can be produced
1058      * sequentially using a {@code for} loop as follows:
1059      * <pre>{@code
1060      *     for (long i = startInclusive; i < endExclusive ; i++) { ... }
1061      * }</pre>
1062      *
1063      * @param startInclusive the (inclusive) initial value
1064      * @param endExclusive the exclusive upper bound
1065      * @return a sequential {@code LongStream} for the range of {@code long}
1066      *         elements
1067      */
range(long startInclusive, final long endExclusive)1068     public static LongStream range(long startInclusive, final long endExclusive) {
1069         if (startInclusive >= endExclusive) {
1070             return empty();
1071         } else if (endExclusive - startInclusive < 0) {
1072             // Size of range > Long.MAX_VALUE
1073             // Split the range in two and concatenate
1074             // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then
1075             // the lower range, [Long.MIN_VALUE, 0) will be further split in two
1076             long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
1077             return concat(range(startInclusive, m), range(m, endExclusive));
1078         } else {
1079             return StreamSupport.longStream(
1080                     new Streams.RangeLongSpliterator(startInclusive, endExclusive, false), false);
1081         }
1082     }
1083 
1084     /**
1085      * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
1086      * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of
1087      * {@code 1}.
1088      *
1089      * @apiNote
1090      * <p>An equivalent sequence of increasing values can be produced
1091      * sequentially using a {@code for} loop as follows:
1092      * <pre>{@code
1093      *     for (long i = startInclusive; i <= endInclusive ; i++) { ... }
1094      * }</pre>
1095      *
1096      * @param startInclusive the (inclusive) initial value
1097      * @param endInclusive the inclusive upper bound
1098      * @return a sequential {@code LongStream} for the range of {@code long}
1099      *         elements
1100      */
rangeClosed(long startInclusive, final long endInclusive)1101     public static LongStream rangeClosed(long startInclusive, final long endInclusive) {
1102         if (startInclusive > endInclusive) {
1103             return empty();
1104         } else if (endInclusive - startInclusive + 1 <= 0) {
1105             // Size of range > Long.MAX_VALUE
1106             // Split the range in two and concatenate
1107             // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
1108             // the lower range, [Long.MIN_VALUE, 0), and upper range,
1109             // [0, Long.MAX_VALUE], will both be further split in two
1110             long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
1111             return concat(range(startInclusive, m), rangeClosed(m, endInclusive));
1112         } else {
1113             return StreamSupport.longStream(
1114                     new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false);
1115         }
1116     }
1117 
1118     /**
1119      * Creates a lazily concatenated stream whose elements are all the
1120      * elements of the first stream followed by all the elements of the
1121      * second stream.  The resulting stream is ordered if both
1122      * of the input streams are ordered, and parallel if either of the input
1123      * streams is parallel.  When the resulting stream is closed, the close
1124      * handlers for both input streams are invoked.
1125      *
1126      * <p>This method operates on the two input streams and binds each stream
1127      * to its source.  As a result subsequent modifications to an input stream
1128      * source may not be reflected in the concatenated stream result.
1129      *
1130      * @implNote
1131      * Use caution when constructing streams from repeated concatenation.
1132      * Accessing an element of a deeply concatenated stream can result in deep
1133      * call chains, or even {@code StackOverflowError}.
1134      *
1135      * @apiNote
1136      * To preserve optimization opportunities this method binds each stream to
1137      * its source and accepts only two streams as parameters.  For example, the
1138      * exact size of the concatenated stream source can be computed if the exact
1139      * size of each input stream source is known.
1140      * To concatenate more streams without binding, or without nested calls to
1141      * this method, try creating a stream of streams and flat-mapping with the
1142      * identity function, for example:
1143      * <pre>{@code
1144      *     LongStream concat = Stream.of(s1, s2, s3, s4).flatMapToLong(s -> s);
1145      * }</pre>
1146      *
1147      * @param a the first stream
1148      * @param b the second stream
1149      * @return the concatenation of the two input streams
1150      */
concat(LongStream a, LongStream b)1151     public static LongStream concat(LongStream a, LongStream b) {
1152         Objects.requireNonNull(a);
1153         Objects.requireNonNull(b);
1154 
1155         Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong(
1156                 a.spliterator(), b.spliterator());
1157         LongStream stream = StreamSupport.longStream(split, a.isParallel() || b.isParallel());
1158         return stream.onClose(Streams.composedClose(a, b));
1159     }
1160 
1161     /**
1162      * A mutable builder for a {@code LongStream}.
1163      *
1164      * <p>A stream builder has a lifecycle, which starts in a building
1165      * phase, during which elements can be added, and then transitions to a built
1166      * phase, after which elements may not be added.  The built phase begins
1167      * begins when the {@link #build()} method is called, which creates an
1168      * ordered stream whose elements are the elements that were added to the
1169      * stream builder, in the order they were added.
1170      *
1171      * @see LongStream#builder()
1172      * @since 1.8
1173      */
1174     public interface Builder extends LongConsumer {
1175 
1176         /**
1177          * Adds an element to the stream being built.
1178          *
1179          * @throws IllegalStateException if the builder has already transitioned
1180          * to the built state
1181          */
1182         @Override
accept(long t)1183         void accept(long t);
1184 
1185         /**
1186          * Adds an element to the stream being built.
1187          *
1188          * @implSpec
1189          * The default implementation behaves as if:
1190          * <pre>{@code
1191          *     accept(t)
1192          *     return this;
1193          * }</pre>
1194          *
1195          * @param t the element to add
1196          * @return {@code this} builder
1197          * @throws IllegalStateException if the builder has already transitioned
1198          * to the built state
1199          */
add(long t)1200         default Builder add(long t) {
1201             accept(t);
1202             return this;
1203         }
1204 
1205         /**
1206          * Builds the stream, transitioning this builder to the built state.
1207          * An {@code IllegalStateException} is thrown if there are further
1208          * attempts to operate on the builder after it has entered the built
1209          * state.
1210          *
1211          * @return the built stream
1212          * @throws IllegalStateException if the builder has already transitioned
1213          * to the built state
1214          */
build()1215         LongStream build();
1216     }
1217 
1218     /**
1219      * Represents an operation that accepts a {@code long}-valued argument
1220      * and a LongConsumer, and returns no result. This functional interface is
1221      * used by {@link LongStream#mapMulti(LongStream.LongMapMultiConsumer) LongStream.mapMulti}
1222      * to replace a long value with zero or more long values.
1223      *
1224      * <p>This is a <a href="../function/package-summary.html">functional interface</a>
1225      * whose functional method is {@link #accept(long, LongConsumer)}.
1226      *
1227      * @see LongStream#mapMulti(LongStream.LongMapMultiConsumer)
1228      *
1229      * @since 16
1230      */
1231     @FunctionalInterface
1232     interface LongMapMultiConsumer {
1233 
1234         /**
1235          * Replaces the given {@code value} with zero or more values by feeding the mapped
1236          * values to the {@code lc} consumer.
1237          *
1238          * @param value the long value coming from upstream
1239          * @param lc a {@code LongConsumer} accepting the mapped values
1240          */
accept(long value, LongConsumer lc)1241         void accept(long value, LongConsumer lc);
1242     }
1243 }
1244