Currently whenever I need to create stream from an array, I do
目前,每当我需要从数组中创建流时,我都会这样做
String[] array = {"x1", "x2"};
Arrays.asList(array).stream();
Is there some direct way to create stream from an array?
是否有直接的方法从数组中创建流?
4 个解决方案
#1
169
You can use Arrays.stream E.g.
您可以使用数组。如流。
Arrays.stream(array);
You can also use Stream.of
as mentioned by @fge , which looks like
您还可以使用流。@fge提到的
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.
但是要注意,Stream.of(intArray)将返回流
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.
更新:正如Stuart Marks所提到的,数组的子程序重载。与使用stream .of .skip(n).limit(m)相比,流更可取,因为前者会产生一个大小的流,而后者不会。原因是限制(m)不知道大小是m还是小于m,而数组。stream进行范围检查并知道流的确切大小,您可以在这里读取array. stream .stream(数组,start,end)实现的源代码,而对于stream .of(数组).skip().limit()返回的流实现,则在这个方法中。
#2
40
Alternative to @sol4me's solution:
替代@sol4me的解决办法:
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.
当光线很长时,它会返回一条长流。另一方面,Stream.of()将返回一个带单个元素的流
#3
13
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.
对于原始类型,使用IntStream。或LongStream。的等。
#4
0
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)
#1
169
You can use Arrays.stream E.g.
您可以使用数组。如流。
Arrays.stream(array);
You can also use Stream.of
as mentioned by @fge , which looks like
您还可以使用流。@fge提到的
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.
但是要注意,Stream.of(intArray)将返回流
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.
更新:正如Stuart Marks所提到的,数组的子程序重载。与使用stream .of .skip(n).limit(m)相比,流更可取,因为前者会产生一个大小的流,而后者不会。原因是限制(m)不知道大小是m还是小于m,而数组。stream进行范围检查并知道流的确切大小,您可以在这里读取array. stream .stream(数组,start,end)实现的源代码,而对于stream .of(数组).skip().limit()返回的流实现,则在这个方法中。
#2
40
Alternative to @sol4me's solution:
替代@sol4me的解决办法:
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.
当光线很长时,它会返回一条长流。另一方面,Stream.of()将返回一个带单个元素的流
#3
13
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.
对于原始类型,使用IntStream。或LongStream。的等。
#4
0
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)