Stream of a reverse list

Dan Smith daniel.smith at oracle.com
Mon Jul 15 11:19:16 PDT 2013


On Jul 15, 2013, at 7:53 AM, Remi Forax <forax at univ-mlv.fr> wrote:

> On 07/15/2013 03:36 PM, Brian Goetz wrote:
>> We did consider such a stream op and triaged it away as bring too niche. It also requires a full barrier to get the first element.  And for infinite streams obviously blows up.
>> 
>> Given that it always requires a full barrier, toArray seems the best way to go.
>> 
>> Sent from my iPad
> 
> We reject having a method reverse() on Stream but not the fact that one can create a Stream that will iterate over the list in a backward way.
> But perhaps, it's better to have a method of List named reverseList() that return a reverse view of the list
> and calls stream() on it.

Has to be prioritized, of course, but I think 'List.reverseStream' is in principle a good suggestion.  Looks a lot like the idea of having different methods on CharSequence to get char-based and int-based views of the same data.  In this case, we're getting front-to-back and back-to-front views of the List.

Collection is _not_ a good place to put a method like this, because Collections are not designed to support reverse-order traversal.  Lists are (see ListIterator).  As are Deques (see Deque.desendingIterator).  (I was surprised, actually, to not find a similar List.reverseIterator method -- I guess the intended idiom is to call 'list.iterator(list.size())' and then iterate with 'ListIterator.previous'.)

—Dan


More information about the lambda-libs-spec-experts mailing list