Java 如何从数组创建流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27888429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a stream from an array?
提问by adam.kubi
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?
有没有一些直接的方法可以从数组创建流?
采纳答案by sol4me
You can use Arrays.stream E.g.
你可以使用 Arrays.stream 例如
Arrays.stream(array);
You can also use Stream.of
as mentioned by @fge , which looks like
您也可以使用Stream.of
@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.
但是 noteStream.of(intArray)
将返回Stream<int[]>
而Arrays.stream(intArr)
将返回IntStream
提供您传递类型的数组int[]
。因此,简而言之,对于基元类型,您可以观察两种方法之间的区别,例如
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
当您将原始数组传递给 时Arrays.stream
,将调用以下代码
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
并且当您将原始数组传递给Stream.of
以下代码时会被调用
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 Markscomment
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.
更新:正如斯图尔特·马克斯评论所提到的,子范围重载Arrays.stream
比使用更可取,Stream.of(array).skip(n).limit(m)
因为前者会产生 SIZED 流,而后者不会。原因是limit(m)
不知道大小是m还是小于m,而Arrays.stream
范围检查并知道流的确切大小您可以阅读Arrays.stream(array,start,end)
here返回的流实现的源代码,而返回的流实现Stream.of(array).skip().limit()
是内这种方法。
回答by fge
Alternative to @sol4me's solution:
@sol4me 解决方案的替代方案:
Stream.of(theArray)
Of the difference between this and Arrays.stream()
: it doesmake a difference if your array is of a primitive type. For instance, if you do:
这与Arrays.stream()
:之间的区别:如果您的数组是原始类型,它确实会有所不同。例如,如果你这样做:
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.
这里someArray
是一个long[]
,它会返回一个LongStream
。Stream.of()
,另一方面,将返回Stream<long[]>
带有单个元素的 a 。
回答by Dima
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.of
或LongStream.of
等。
回答by merqlove
You can make it also by low level method which has parallel option:
您也可以通过具有并行选项的低级方法来制作它:
Update: Use full array.length (not length - 1).
更新:使用完整的 array.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)
回答by vipul patel
You can use Arrays.stream :
您可以使用 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
这可以确保根据您的数组输入类型确定蒸汽的返回类型,如果它String []
然后返回Stream<String>
,如果int []
然后返回IntStream
When you already know input type array then good to use specific one like for input type int[]
当您已经知道输入类型数组时,最好使用特定的数组,例如输入类型 int[]
IntStream.of(array);
This returns Intstream.
这将返回 Instream。
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.
在第一个示例中,Java 使用方法overloading
根据输入类型查找特定方法,而在第二个示例中,您已经知道输入类型并调用特定方法。
回答by Kaplan
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();