3

I wonder why Java 8 does not include support to use streams on arrays the same way it does on collections. You can write

    Collection<String> myCollection = new ArrayList<String>();
    Stream<String> stream = myCollection.stream();

but you cannot write:

    String[] myArray = new String[] {};
    Stream<String> stream = myArray.stream();

I am aware of the utility method Arrays.stream, so this is can be used instead:

    Stream<String> stream = Arrays.stream(myArray);

But it leaves me wondering why this clumsy call of a static utility method is required in a context where lambda expression allow to simply code so much. I am aware of the significant difference between the (object/interface based) world of Collection and the more "native" world of array.

Who can elaborate on this?

2
  • Someone will answer better than me but arrays are special Object in Java, this is not from a specific Class/Interface like Collection. You don't have Array<String> but String[], this is specific to arrays Commented Jan 2, 2017 at 12:08
  • 2
    Why stream in particular? What about other methods in the Collection interface? Commented Jan 2, 2017 at 12:09

3 Answers 3

6

In order to add methods to arrays, the language, not the library, needs to be changed. Unlike "regular" objects with members defined in class files, arrays have their members defined directly in the language specification.

This means that adding myArray.stream() method would require changing the language specification, and adding support to the compiler. This approach is a lot more complex than adding Arrays.stream(myArray), which can be implemented entirely within Java class library with very little effort.

Sign up to request clarification or add additional context in comments.

Comments

3

Something very close to your suggestion was discussed but the feature request was declined:

What you are suggesting is a not-small language feature to provide a small bit of sugar to something you can already easily do with the libraries. So, while we've considered the possibility of adding a mechanism for injecting methods into arrays, the ease of just wrapping the array in this manner suggests we are probably better off spending our complexity budget elsewhere, unless we can derive some other larger benefit from this.

2 Comments

Thanks for that link! In that discussion I also found this hint very helpful: The main issue is that currently there is no 'real' method on an array type, so usually VMs have a special way to represent them,what you propose does require big changes from the VM point of view.
On second thought and further research this quote is actualy wrong! There is no need to change the VMs. It can be handled by the compiler. And if I am not mistaken this is already used by Java 8 for the :: method-as-a-parameter language extension.
1

The arrays are specified fully in the Java Language Specfication. Where as all Collection classes are only part of the API. And as you might imagine changes to the specification are more expensive to make than changes to the API.

What you suggest would not just mean an addition to the specification, but also that the class Stream is known in the language specification. While this is technically possible (see for instance Class and Object), this is not very common. Note even the Collection classes are part of the language specification.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.