What are fail-safe & fail-fast Iterators in Java

后端 未结 4 986
醉话见心
醉话见心 2020-11-29 15:11

There are two types of iterators in Java: fail-safe and fail-fast.

What does this mean, and is the difference between them?

4条回答
  •  余生分开走
    2020-11-29 15:59

    They are rather fail-fast and weakly-consistent types:

    Iterators from java.util package throw ConcurrentModificationException if collection was modified by collection's methods (add / remove) while iterating

    Iterators from java.util.concurrent package typically iterate over a snapshot and allow concurrent modifications but may not reflect collection updates after the iterator was created.

提交回复
热议问题