Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

You can use Arrays.stream E.g.

Arrays.stream(array);

EDIT

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 Arrys.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.

share|improve this answer
6  
This answer is better because Arrays.stream has all the overloaded cases for primitive arrays. I.e Stream.of(new int[]{1,2,3}) will give you a Stream<int[]> whereas Arrays.stream will give you back an IntStream which is probably what you want. So +1 –  user2336315 16 hours ago
2  
@mrres1 See the updated answer –  sol4me 16 hours ago
2  
@Dima I was answering the "How do I create a Stream from an array" My example is simplified to fit in the comment (and yes in this case, I would call also of with the values), but you could have defined the int array before wanting to transform it in a Stream. That's why Arrays.stream is better for transforming an array into a Stream (and my example was to show the difference between both, which was not mentioned in the answer at first). –  user2336315 15 hours ago
2  
@Dima I guess it's a matter of taste. I mean better in a sense Stream.of could give you some surprises (like when you call Arrays.asList with a primitive array and that people expect a List<Integer> back) :-) –  user2336315 15 hours ago
2  
Arrays.stream supports streaming a range of the array, which IntStream.of doesn’t. In contrast, Stream.of is the better choice if you want a Stream<int[]> of size 1 –  Holger 15 hours ago

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.

share|improve this answer
1  
@zeroflagL oops, true; corrected. –  fge 16 hours ago
1  
@Dima sure, but Arrays.stream() works for that as well –  fge 15 hours ago
1  
@Dima that's your opinion. That's not mine... –  fge 15 hours ago
1  
Well, as to streams, convenience! No need to call *Stream.of() when you have Arrays.stream() when dealing with primitive arrays. And as to arrays not being real objects, well, this is Java, this has been the case since 1.0 so deal with it; brooding over this helps nothing –  fge 14 hours ago
1  
@Dima and so is yours; you consider Arrays.stream() not to be convenient, I consider it to be convenient. Enough said. –  fge 13 hours ago
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.