How can I create a stream from an array?

后端 未结 6 2284
挽巷
挽巷 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 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 with a single element.

提交回复
热议问题