How can I create a stream from an array?

后端 未结 6 2257
挽巷
挽巷 2020-11-27 03:18

Currently whenever I need to create stream from an array, I do

String[] array = {\"x1\", \"x2\"};
Arrays.asList(array).stream();

Is there s

相关标签:
6条回答
  • 2020-11-27 03:51
    Stream.of("foo", "bar", "baz")
    

    Or, if you are already have an array, you can also do

    Stream.of(array) 
    

    For primitive types use IntStream.of or LongStream.of etc.

    0 讨论(0)
  • 2020-11-27 03:53

    You can use Arrays.stream :

    Arrays.stream(array); 

    This ensures the return type of steam based on your array input type if its String [] then return Stream<String>, if int [] then returns IntStream

    When you already know input type array then good to use specific one like for input type int[]

     IntStream.of(array); 

    This returns Intstream.

    In first example, Java uses method overloading to find specific method based on input types while as in second you already know the input type and calling specific method.

    0 讨论(0)
  • 2020-11-27 04:09

    You can use Arrays.stream E.g.

    Arrays.stream(array);
    

    You can also use Stream.of as mentioned by @fge , which looks like

    public static<T> Stream<T> of(T... values) {
        return Arrays.stream(values);
    }
    

    But note Stream.of(intArray) will return Stream<int[]> whereas Arrays.stream(intArr) will return IntStream providing you pass an array of type int[]. So in a nutshell for primitives type you can observe the difference between 2 methods E.g.

    int[] arr = {1, 2};
    Stream<int[]> arr1 = Stream.of(arr);
    
    IntStream stream2 = Arrays.stream(arr); 
    

    When you pass primitive array to Arrays.stream, the following code is invoked

    public static IntStream stream(int[] array) {
        return stream(array, 0, array.length);
    }
    

    and when you pass primitive array to Stream.of the following code is invoked

     public static<T> Stream<T> of(T t) {
         return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
     }
    

    Hence you get different results.

    Updated: As mentioned by Stuart Marks comment The subrange overload of Arrays.stream is preferable to using Stream.of(array).skip(n).limit(m) because the former results in a SIZED stream whereas the latter does not. The reason is that limit(m) doesn't know whether the size is m or less than m, whereas Arrays.stream does range checks and knows the exact size of the stream You can read the source code for stream implementation returned by Arrays.stream(array,start,end) here, whereas for stream implementation returned by Stream.of(array).skip().limit() is within this method.

    0 讨论(0)
  • 2020-11-27 04:10

    You can make it also by low level method which has parallel option:

    Update: Use full array.length (not length - 1).

    /** 
     * Creates a new sequential or parallel {@code Stream} from a
     * {@code Spliterator}.
     *
     * <p>The spliterator is only traversed, split, or queried for estimated
     * size after the terminal operation of the stream pipeline commences.
     *
     * @param <T> the type of stream elements
     * @param spliterator a {@code Spliterator} describing the stream elements
     * @param parallel if {@code true} then the returned stream is a parallel
     *        stream; if {@code false} the returned stream is a sequential
     *        stream.
     * @return a new sequential or parallel {@code Stream}
     *
     * <T> Stream<T> stream(Spliterator<T> spliterator, boolean parallel)
     */
    
    StreamSupport.stream(Arrays.spliterator(array, 0, array.length), true)
    
    0 讨论(0)
  • 2020-11-27 04:17

    Alternative to @sol4me's solution:

    Stream.of(theArray)
    

    Of the difference between this and Arrays.stream(): it does make a difference if your array is of a primitive type. For instance, if you do:

    Arrays.stream(someArray)
    

    where someArray is a long[], it will return a LongStream. Stream.of(), on the other hand, will return a Stream<long[]> with a single element.

    0 讨论(0)
  • 2020-11-27 04:18

    rarely seen, but this is the directest way

    Stream.Builder<String> builder = Stream.builder();
    for( int i = 0; i < array.length; i++ )
      builder.add( array[i] );
    Stream<String> stream = builder.build();
    
    0 讨论(0)
提交回复
热议问题