< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2012, 2015, 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


1185      */
1186     public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
1187         Objects.requireNonNull(f);
1188         final Iterator<T> iterator = new Iterator<T>() {
1189             @SuppressWarnings("unchecked")
1190             T t = (T) Streams.NONE;
1191 
1192             @Override
1193             public boolean hasNext() {
1194                 return true;
1195             }
1196 
1197             @Override
1198             public T next() {
1199                 return t = (t == Streams.NONE) ? seed : f.apply(t);
1200             }
1201         };
1202         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
1203                 iterator,
1204                 Spliterator.ORDERED | Spliterator.IMMUTABLE), false);



































































1205     }
1206 
1207     /**
1208      * Returns an infinite sequential unordered stream where each element is
1209      * generated by the provided {@code Supplier}.  This is suitable for
1210      * generating constant streams, streams of random elements, etc.
1211      *
1212      * @param <T> the type of stream elements
1213      * @param s the {@code Supplier} of generated elements
1214      * @return a new infinite sequential unordered {@code Stream}
1215      */
1216     public static<T> Stream<T> generate(Supplier<T> s) {
1217         Objects.requireNonNull(s);
1218         return StreamSupport.stream(
1219                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
1220     }
1221 
1222     /**
1223      * Creates a lazily concatenated stream whose elements are all the
1224      * elements of the first stream followed by all the elements of the


   1 /*
   2  * Copyright (c) 2012, 2016, 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


1185      */
1186     public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
1187         Objects.requireNonNull(f);
1188         final Iterator<T> iterator = new Iterator<T>() {
1189             @SuppressWarnings("unchecked")
1190             T t = (T) Streams.NONE;
1191 
1192             @Override
1193             public boolean hasNext() {
1194                 return true;
1195             }
1196 
1197             @Override
1198             public T next() {
1199                 return t = (t == Streams.NONE) ? seed : f.apply(t);
1200             }
1201         };
1202         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
1203                 iterator,
1204                 Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
1205     }
1206 
1207     /**
1208      * Returns a sequential ordered {@code Stream} produced by iterative
1209      * application of a function {@code f} to an initial element {@code seed},
1210      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
1211      * {@code f(f(seed))}, etc.  The stream terminates when {@code predicate}
1212      * returns false.
1213      *
1214      * <p>The first element (position {@code 0}) in the {@code Stream} will be
1215      * the provided {@code seed}.  For {@code n > 0}, the element at position
1216      * {@code n}, will be the result of applying the function {@code f} to the
1217      * element at position {@code n - 1}.
1218      *
1219      * @param <T> the type of stream elements
1220      * @param seed the initial element
1221      * @param predicate a predicate to apply to elements to determine when the 
1222      *          stream must terminate.
1223      * @param f a function to be applied to the previous element to produce
1224      *          a new element
1225      * @return a new sequential {@code Stream}
1226      * @since 9
1227      */
1228     public static<T> Stream<T> iterate(T seed, Predicate<T> predicate, UnaryOperator<T> f) {
1229         Objects.requireNonNull(f);
1230         Objects.requireNonNull(predicate);
1231         Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, 
1232                Spliterator.ORDERED | Spliterator.IMMUTABLE) {
1233             T prev;
1234             boolean started, finished;
1235 
1236             @Override
1237             public boolean tryAdvance(Consumer<? super T> action) {
1238                 Objects.requireNonNull(action);
1239                 if (finished)
1240                     return false;
1241                 T t;
1242                 if (started)
1243                     t = f.apply(prev);
1244                 else {
1245                     t = seed;
1246                     started = true;
1247                 }
1248                 if (!predicate.test(t)) {
1249                     prev = null;
1250                     finished = true;
1251                     return false;
1252                 }
1253                 action.accept(prev = t);
1254                 return true;
1255             }
1256 
1257             @Override
1258             public void forEachRemaining(Consumer<? super T> action) {
1259                 Objects.requireNonNull(action);
1260                 if (finished)
1261                     return;
1262                 T t = started ? f.apply(prev) : seed;
1263                 finished = true;
1264                 prev = null;
1265                 while (predicate.test(t)) {
1266                     action.accept(t);
1267                     t = f.apply(t);
1268                 }
1269             }
1270         };
1271         return StreamSupport.stream(spliterator, false);
1272     }
1273 
1274     /**
1275      * Returns an infinite sequential unordered stream where each element is
1276      * generated by the provided {@code Supplier}.  This is suitable for
1277      * generating constant streams, streams of random elements, etc.
1278      *
1279      * @param <T> the type of stream elements
1280      * @param s the {@code Supplier} of generated elements
1281      * @return a new infinite sequential unordered {@code Stream}
1282      */
1283     public static<T> Stream<T> generate(Supplier<T> s) {
1284         Objects.requireNonNull(s);
1285         return StreamSupport.stream(
1286                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
1287     }
1288 
1289     /**
1290      * Creates a lazily concatenated stream whose elements are all the
1291      * elements of the first stream followed by all the elements of the


< prev index next >