RFR: 8005051: default methods for Iterator

Vitaly Davidovich vitalyd at gmail.com
Fri Dec 14 14:38:34 UTC 2012

One interesting thing for me here is .net's IEnumerable (akin to Iterable)
never received a ForEach extension method - this method only exists on
their List<T> class.  From what I gather this was to avoid passing a block
(Action in .net) that modifies the underlying collection (e.g removes an
item).  I never really thought that paranoia was worth it since most uses
will not want to mutate.  I'm glad Iterable will have this, but curious if
anyone thought about this case explicitly.


Sent from my phone
On Dec 14, 2012 8:38 AM, "Peter Levart" <peter.levart at gmail.com> wrote:

> Hi,
> Iterator.forEach(Block) does not specify anything about the order of
> internal iteration in correspondence to the order of classical external
> iteration (hasNext()/next()). I think that if the order must be the same
> then Javadoc should mention it. If the orders are allowed be different,
> then Javadoc should mention it too.
> As for the clash with Iterable.forEach(Block): The name "forEach" suggests
> just that - each element will be passed to Block. Nothing is said about the
> order of elements or even Threads. I dont't think it should be, since
> Iterables can be various kinds.
> But Iterator is a one-shot single-threaded API and it's hard to imagine
> the implementation where internal iteration would want to be different than
> external. So the Iterator method be better called differently. What about
> Iterator.iterate(Block) ?
> Regards, Peter
> On 12/14/2012 02:24 AM, Akhil Arora wrote:
>> As part of the library lambdafication, this patch adds a forEach default
>> method to Iterator, and converts remove() into a default method so that
>> implementations of Iterator no longer have to override remove if they
>> desire the default behavior, which is to throw an
>> UnsupportedOperationException.
>> http://cr.openjdk.java.net/~**akhil/8005051.0/webrev/<http://cr.openjdk.java.net/~akhil/8005051.0/webrev/>
>> The above patch requires a small patch to an internal class which happens
>> to implement both Iterable and Iterator. Now both Iterable and Iterator
>> supply a default forEach method, so the compiler balks. One minimally
>> intrusive solution is for this class to override both defaults and provide
>> its own version of forEach.
>> http://cr.openjdk.java.net/~**akhil/8005053.0/webrev/<http://cr.openjdk.java.net/~akhil/8005053.0/webrev/>
>> Please review
>> Thanks

More information about the core-libs-dev mailing list