Why iterator.forEachRemaining doesnt remove element in the Consumer lambda?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 18:45:49

Updated thanks to @studro. See his comment below.

The API documentation states:

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

It seems like the "unspecified behavior" part also applies during this internal iteration.

Granted, the documentation for forEachRemaining states that the behavior is equivalent to

while (hasNext())
    action.accept(next());

and if action::accept did in fact call iterator.remove() the above snippet should not throw any exception (if remove is a supported operation). This might be a documentation bug.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!