< prev index next >

src/java.base/share/classes/java/util/stream/Stream.java

Print this page
rev 53930 : 8148917: enhanced-for statement should allow streams
Reviewed-by: XXX

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -101,11 +101,15 @@
  * provide a means to directly access or manipulate their elements, and are
  * instead concerned with declaratively describing their source and the
  * computational operations which will be performed in aggregate on that source.
  * However, if the provided stream operations do not offer the desired
  * functionality, the {@link #iterator()} and {@link #spliterator()} operations
- * can be used to perform a controlled traversal.
+ * can be used to perform a controlled traversal. Stream implements the
+ * {@link IterableOnce} interface, allowing a stream instance to be used
+ * in an enhanced-for ("for each") statement. Note that this statement implicitly
+ * calls the {@code iterator()} method, which consumes the stream. The stream
+ * thus cannot be used for any subsequent operations.
  *
  * <p>A stream pipeline, like the "widgets" example above, can be viewed as
  * a <em>query</em> on the stream source.  Unless the source was explicitly
  * designed for concurrent modification (such as a {@link ConcurrentHashMap}),
  * unpredictable or erroneous behavior may result from modifying the stream

@@ -162,11 +166,19 @@
  * @see IntStream
  * @see LongStream
  * @see DoubleStream
  * @see <a href="package-summary.html">java.util.stream</a>
  */
-public interface Stream<T> extends BaseStream<T, Stream<T>> {
+public interface Stream<T> extends BaseStream<T, Stream<T>>, IterableOnce<T> {
+
+    // Need to re-abstract spliterator() to avoid inheriting the default
+    // method from Iterable.
+    /**
+     * {@inheritDoc}
+     * @return {@inheritDoc}
+     */
+    Spliterator<T> spliterator();
 
     /**
      * Returns a stream consisting of the elements of this stream that match
      * the given predicate.
      *
< prev index next >