Return types array/list/stream [was: API question for point lambdafication]
scolebourne at joda.org
Wed Feb 20 08:47:41 PST 2013
On 20 February 2013 16:35, Brian Goetz <brian.goetz at oracle.com> wrote:
>> My preference would be as folows:
>> - only use array as a return type to match other methods in the same type
>> - prefer List to Set or Collection
>> - prefer Stream to Iterator and Enumeration (and probably Iterable)
>> - prefer List to Stream for small/bounded data sets
>> Thus, on the last point, I would argue that String.splitAsStream() is
>> wrong, and should be String.splitToList(). (As a side note, with my
>> general "arrays are bad" hat on, returning an array here was another
>> bad choice)
>> Users wanting a stream can easily get it from the list. The reverse is
>> not so true, and almost certainly more wasteful.
> This ignores a significant consideration -- List requires buffering all the
> matches before any work can be done, whereas Stream is lazy. What if the
> List is potentially huge (or infinite)? What if you're only going to look
> at the first few? What if producing elements is expensive? What if you
> have an expensive operation to perform on the results, and want to do so in
> parallel? In all of these cases, preferring List over Stream gives up
> significant performance opportunities. And, constructing lists is likely to
> be more expensive than constructing streams.
> The argument that we should "prefer List to Stream because you can always
> get a stream from a list" is too simplistic.
And I didn't say that... I said "prefer List to Stream for
small/bounded data sets"
> Regex parsing is a good example of where you might want a stream rather than
> a list, as you might well not consume all the matches.
My regex usage tends to be on small (<128 character, or a typical
"line") strings where I want to extract out groups of text. I can
always cope with the entire list of results, and I often want to
access them in random order. For my use case, returning a list would
often have been more useful than an array.
I still say that the original question stands. And I ask it because I
think, like all new features, there is a danger of over-use. Laying
down some guidelines on usage is a Good Thing IMO. Stream really
shouldn't be the default return type - List is frequently much more
useful to clients.
More information about the lambda-dev