4

I understand that Java 8 Stream provides .collect method to convert a Stream to any collection / data Structure of our choice, and it is very generic in nature. Understand that Stream might be originated from non-collection object / resouce. But from usage perspective, it is pretty natural to convert Stream to List / Set.

Stream already provides toArray() method to convert to array, so why not toList() and toSet()?

Mrinal
  • 1,846
  • 14
  • 17
  • [Retrieving a List from Stream](http://stackoverflow.com/questions/14830313/retrieving-a-list-from-a-java-util-stream-stream-in-java8) is closely related. – Mick Mnemonic Jul 19 '15 at 13:06
  • 5
    It's a compromise between pollution of the interface and ease of use. They chose not to pollute the interface more. Adding toList() would ask for a toSet() method as well, then a toMap(), etc. etc., ultimetely repeating many methods of Collectors, and making the Stream interface more complex and "polluted" with many collecting methods. – JB Nizet Jul 19 '15 at 13:10
  • http://stackoverflow.com/questions/28782165/why-didnt-stream-have-a-tolist-method – Alex - GlassEditor.com Jul 19 '15 at 13:17
  • 2
    The `toArray()` method could easily be implemented as `Collector` as well. I guess, it's added directly to the Stream interface for efficiency reasons: if your Stream is sized, you can take an advantage of it preallocating the array of exact size. This is currently impossible for collectors: they are unable to get the size of the stream in advance. There's a [request](https://bugs.openjdk.java.net/browse/JDK-8072840) to fix this, though no work seems to be done yet in this direction. – Tagir Valeev Jul 19 '15 at 15:04
  • @TagirValeev I have one related question about `.toArray`. Why does it return `Object[]` , not `T[]` ? – Mrinal Jul 19 '15 at 17:53
  • @MrinalK.Samanta: because due to Java generics limitations you cannot create an array of unknown type. There's `toArray` with function parameter which returns the array of any type (for example, `toArray(String[]::new)`). – Tagir Valeev Jul 20 '15 at 03:07
  • @Tagir Brian Goetz additionally argued that arrays are a native special case in Java so they deserve their method; the same argument does not propagate to all the Collections Framework types. – Marko Topolnik Jul 20 '15 at 11:32

1 Answers1

6

Because you wouldn't know what the List implementation would be like:

  • would it be mutable?
  • would you be able to change its size?
  • etc etc.

In fact, even Collectors.toList() makes no guarantee:

There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).

As such, providing a generic .toList() method would fit some usages for some people, but it would never be able to fit all usages for all people. And of course, the same applies to Collectors.toSet().

Arrays are not a problem however: you know what you can do with them.

fge
  • 119,121
  • 33
  • 254
  • 329
  • 2
    Why would this argument apply against having a `toList` `Stream` method and not against having a `toList()` Collector to be used in the Stream's `collect` method? – Eran Jul 19 '15 at 13:09
  • 2
    @Eran well, another reason is that depending on the `Stream` implementation you may have lists returned with different characteristics, unless the contract of the method is well defined. As such the decision makes sense imho. – fge Jul 19 '15 at 13:15