Possible JSR-310 (date/time API) stream methods

Paul Sandoz paul.sandoz at oracle.com
Fri Mar 8 03:05:46 PST 2013


On Mar 8, 2013, at 11:14 AM, Stephen Colebourne <scolebourne at joda.org> wrote:

> On 7 March 2013 20:44, Brian Goetz <brian.goetz at oracle.com> wrote:
>> I think a useful mental model for "can I easily make a stream out of this"
>> is "can I easily make an Iterator for this."  (Of course, there are other
>> ways to construct a stream other than an Iterator.)
>> 
>> So if you are tempted to write methods that would return an array or a
>> Collection, consider whether instead (or in addition) to return a Stream.
> 
> Which comes back to an earlier question. Why should I return a Stream?
> If I return an Iterator then that is more widely applicable and can be
> converted to a stream is necessary...
> 

Iterator is a rather a poor interface to do anything directly with since it is the LCD of traversal. So i would favour Stream over Iterator in terms of providing something useful to consumers of an API.

The use of an iterator can be viewed as very convenient implementation detail for creating a Stream. In addition to the iterator the size may be known. Certain other characteristics may be known, such as whether the source of elements has an encounter order, or an encounter order that has a sort order, or the elements are distinct etc (see Spliterator characteristics for more details).

So if you expose just an Iterator you may loose important information and still there is a logical gap that developers need to bridge to convert to a Stream. Where possible that gap should be bridged by the framework developers.

Paul.


More information about the lambda-dev mailing list