RFR: 8005051: default methods for Iterator

Peter Levart peter.levart at gmail.com
Fri Dec 14 13:38:06 UTC 2012


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/
> 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/
> Please review
> Thanks

More information about the core-libs-dev mailing list