src/share/classes/java/util/Arrays.java

Print this page
rev 6979 : Arrays.stream
rev 6980 : [mq]: 7b.JDK-8011918

@@ -23,11 +23,25 @@
  * questions.
  */
 
 package java.util;
 
-import java.lang.reflect.*;
+import java.lang.reflect.Array;
+import java.util.concurrent.ForkJoinPool;
+import java.util.function.BinaryOperator;
+import java.util.function.DoubleBinaryOperator;
+import java.util.function.IntBinaryOperator;
+import java.util.function.IntFunction;
+import java.util.function.IntToDoubleFunction;
+import java.util.function.IntToLongFunction;
+import java.util.function.IntUnaryOperator;
+import java.util.function.LongBinaryOperator;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 import static java.util.ArraysParallelSortHelpers.*;
 
 /**
  * This class contains various methods for manipulating arrays (such as
  * sorting and searching). This class also contains a static factory

@@ -4304,41 +4318,188 @@
         }
         buf.append(']');
         dejaVu.remove(a);
     }
 
+
+    /**
+     * Set all elements of the specified array, using the provided
+     * generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, it is relayed to
+     * the caller and the array is left in an indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @param <T> Type of elements of the array
+     * @throw NullPointerException if the generator is null
+     */
+    public static<T> void setAll(T[] array, IntFunction<? extends T> generator) {
+        Objects.requireNonNull(generator);
+        for (int i=0; i<array.length; i++)
+            array[i] = generator.apply(i);
+    }
+
+    /**
+     * Set all elements of the specified array, in parallel, using the
+     * provided generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, an unchecked exception
+     * is thrown from {@code parallelSetAll} and the array is left in an
+     * indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @param <T> Type of elements of the array
+     * @throw NullPointerException if the generator is null
+     */
+    public static<T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
+        Objects.requireNonNull(generator);
+        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
+    }
+
+    /**
+     * Set all elements of the specified array, using the provided
+     * generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, it is relayed to
+     * the caller and the array is left in an indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @throw NullPointerException if the generator is null
+     */
+    public static void setAll(int[] array, IntUnaryOperator generator) {
+        Objects.requireNonNull(generator);
+        for (int i=0; i<array.length; i++)
+            array[i] = generator.applyAsInt(i);
+    }
+
+    /**
+     * Set all elements of the specified array, in parallel, using the
+     * provided generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, an unchecked exception
+     * is thrown from {@code parallelSetAll} and the array is left in an
+     * indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     * value for that position
+     * @throw NullPointerException if the generator is null
+     */
+    public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
+        Objects.requireNonNull(generator);
+        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
+    }
+
+    /**
+     * Set all elements of the specified array, using the provided
+     * generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, it is relayed to
+     * the caller and the array is left in an indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @throw NullPointerException if the generator is null
+     */
+    public static void setAll(long[] array, IntToLongFunction generator) {
+        Objects.requireNonNull(generator);
+        for (int i=0; i<array.length; i++)
+            array[i] = generator.applyAsLong(i);
+    }
+
     /**
-     * Creates a {@link Spliterator} covering all of the specified array.
+     * Set all elements of the specified array, in parallel, using the
+     * provided generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, an unchecked exception
+     * is thrown from {@code parallelSetAll} and the array is left in an
+     * indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @throw NullPointerException if the generator is null
+     */
+    public static void parallelSetAll(long[] array, IntToLongFunction generator) {
+        Objects.requireNonNull(generator);
+        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
+    }
+
+    /**
+     * Set all elements of the specified array, using the provided
+     * generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, it is relayed to
+     * the caller and the array is left in an indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @throw NullPointerException if the generator is null
+     */
+    public static void setAll(double[] array, IntToDoubleFunction generator) {
+        Objects.requireNonNull(generator);
+        for (int i=0; i<array.length; i++)
+            array[i] = generator.applyAsDouble(i);
+    }
+
+    /**
+     * Set all elements of the specified array, in parallel, using the
+     * provided generator function to compute each element.
+     *
+     * <p>If the generator function throws an exception, an unchecked exception
+     * is thrown from {@code parallelSetAll} and the array is left in an
+     * indeterminate state.
+     *
+     * @param array Array to be initialized
+     * @param generator Function accepting an index and producing the desired
+     *        value for that position
+     * @throw NullPointerException if the generator is null
+     */
+    public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
+        Objects.requireNonNull(generator);
+        IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
+    }
+
+    /**
+     * Returns a {@link Spliterator} covering all of the specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param <T> Type of elements
      * @param array The array, assumed to be unmodified during use
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @since 1.8
      */
     public static <T> Spliterator<T> spliterator(T[] array) {
         return Spliterators.spliterator(array,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator} covering the specified range of the
+     * Returns a {@link Spliterator} covering the specified range of the
      * specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param <T> Type of elements
      * @param array The array, assumed to be unmodified during use
      * @param fromIndex The least index (inclusive) to cover
      * @param toIndex One past the greatest index to cover
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
      *         {@code toIndex} is less than {@code fromIndex}, or
      *         {@code toIndex} is greater than the array size
      * @since 1.8

@@ -4347,38 +4508,38 @@
         return Spliterators.spliterator(array, fromIndex, toIndex,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator.OfInt} covering all of the specified array.
+     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param array The array, assumed to be unmodified during use
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @since 1.8
      */
     public static Spliterator.OfInt spliterator(int[] array) {
         return Spliterators.spliterator(array,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator.OfInt} covering the specified range of the
+     * Returns a {@link Spliterator.OfInt} covering the specified range of the
      * specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param array The array, assumed to be unmodified during use
      * @param fromIndex The least index (inclusive) to cover
      * @param toIndex One past the greatest index to cover
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
      *         {@code toIndex} is less than {@code fromIndex}, or
      *         {@code toIndex} is greater than the array size
      * @since 1.8

@@ -4387,38 +4548,38 @@
         return Spliterators.spliterator(array, fromIndex, toIndex,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator.OfLong} covering all of the specified array.
+     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param array The array, assumed to be unmodified during use
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @since 1.8
      */
     public static Spliterator.OfLong spliterator(long[] array) {
         return Spliterators.spliterator(array,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator.OfLong} covering the specified range of the
+     * Returns a {@link Spliterator.OfLong} covering the specified range of the
      * specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param array The array, assumed to be unmodified during use
      * @param fromIndex The least index (inclusive) to cover
      * @param toIndex One past the greatest index to cover
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
      *         {@code toIndex} is less than {@code fromIndex}, or
      *         {@code toIndex} is greater than the array size
      * @since 1.8

@@ -4427,45 +4588,175 @@
         return Spliterators.spliterator(array, fromIndex, toIndex,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator.OfDouble} covering all of the specified
+     * Returns a {@link Spliterator.OfDouble} covering all of the specified
      * array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param array The array, assumed to be unmodified during use
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @since 1.8
      */
     public static Spliterator.OfDouble spliterator(double[] array) {
         return Spliterators.spliterator(array,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
 
     /**
-     * Creates a {@link Spliterator.OfDouble} covering the specified range of
+     * Returns a {@link Spliterator.OfDouble} covering the specified range of
      * the specified array.
      *
      * <p>The spliterator reports {@link Spliterator#SIZED},
      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
      * {@link Spliterator#IMMUTABLE}.
      *
      * @param array The array, assumed to be unmodified during use
      * @param fromIndex The least index (inclusive) to cover
      * @param toIndex One past the greatest index to cover
-     * @return A spliterator from the array
+     * @return the spliterator for the array elements
      * @throws NullPointerException if the specified array is {@code null}
      * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
      *         {@code toIndex} is less than {@code fromIndex}, or
      *         {@code toIndex} is greater than the array size
      * @since 1.8
      */
     public static Spliterator.OfDouble spliterator(double[] array, int fromIndex, int toIndex) {
         return Spliterators.spliterator(array, fromIndex, toIndex,
                                         Spliterator.ORDERED | Spliterator.IMMUTABLE);
     }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param <T> The type of the array elements
+     * @param array The array, assumed to be unmodified during use
+     * @return A {@code Stream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @since 1.8
+     */
+    public static <T> Stream<T> stream(T[] array) {
+        return stream(array, 0, array.length);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with specified range of the
+     * specified array as its source.
+     *
+     * @param <T> The type of the array elements
+     * @param array The array, assumed to be unmodified during use
+     * @param fromIndex The index of the first element (inclusive) to be
+     *        encountered
+     * @param toIndex One past the index of the last element to be encountered
+     * @return A {@code Stream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
+     *         {@code toIndex} is less than {@code fromIndex}, or
+     *         {@code toIndex} is greater than the array size
+     * @since 1.8
+     */
+    public static <T> Stream<T> stream(T[] array, int fromIndex, int toIndex) {
+        return StreamSupport.stream(spliterator(array, fromIndex, toIndex));
+    }
+
+    /**
+     * Returns a sequential {@link IntStream} with the specified array as its
+     * source.
+     *
+     * @param array The array, assumed to be unmodified during use
+     * @return An {@code IntStream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @since 1.8
+     */
+    public static IntStream stream(int[] array) {
+        return stream(array, 0, array.length);
+    }
+
+    /**
+     * Returns a sequential {@link IntStream} with specified range of the
+     * specified array as its source.
+     *
+     * @param array The array, assumed to be unmodified during use
+     * @param fromIndex The index of the first element (inclusive) to be
+     *        encountered
+     * @param toIndex One past the index of the last element to be encountered
+     * @return An {@code IntStream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
+     *         {@code toIndex} is less than {@code fromIndex}, or
+     *         {@code toIndex} is greater than the array size
+     * @since 1.8
+     */
+    public static IntStream stream(int[] array, int fromIndex, int toIndex) {
+        return StreamSupport.intStream(spliterator(array, fromIndex, toIndex));
+    }
+
+    /**
+     * Returns a sequential {@link LongStream} with the specified array as its
+     * source.
+     *
+     * @param array The array, assumed to be unmodified during use
+     * @return A {@code LongStream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @since 1.8
+     */
+    public static LongStream stream(long[] array) {
+        return stream(array, 0, array.length);
+    }
+
+    /**
+     * Returns a sequential {@link LongStream} with specified range of the
+     * specified array as its source.
+     *
+     * @param array The array, assumed to be unmodified during use
+     * @param fromIndex The index of the first element (inclusive) to be
+     *        encountered
+     * @param toIndex One past the index of the last element to be encountered
+     * @return A {@code LongStream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
+     *         {@code toIndex} is less than {@code fromIndex}, or
+     *         {@code toIndex} is greater than the array size
+     * @since 1.8
+     */
+    public static LongStream stream(long[] array, int fromIndex, int toIndex) {
+        return StreamSupport.longStream(spliterator(array, fromIndex, toIndex));
+    }
+
+    /**
+     * Returns a sequential {@link DoubleStream} with the specified array as its
+     * source.
+     *
+     * @param array The array, assumed to be unmodified during use
+     * @return A {@code DoubleStream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @since 1.8
+     */
+    public static DoubleStream stream(double[] array) {
+        return stream(array, 0, array.length);
+    }
+
+    /**
+     * Returns a sequential {@link DoubleStream} with specified range of the
+     * specified array as its source.
+     *
+     * @param array The array, assumed to be unmodified during use
+     * @param fromIndex The index of the first element (inclusive) to be
+     *        encountered
+     * @param toIndex One past the index of the last element to be encountered
+     * @return A {@code DoubleStream} from the array
+     * @throws NullPointerException if the specified array is {@code null}
+     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex} is negative,
+     *         {@code toIndex} is less than {@code fromIndex}, or
+     *         {@code toIndex} is greater than the array size
+     * @since 1.8
+     */
+    public static DoubleStream stream(double[] array, int fromIndex, int toIndex) {
+        return StreamSupport.doubleStream(spliterator(array, fromIndex, toIndex));
+    }
 }