java - 迭代时向 ConcurrentSkipListSet 删除和添加元素

标签 java collections

在迭代 ConcurrentSkipListSet 时删除和添加元素是否安全:

ConcurrentSkipListSet<Element> set = new ConcurrentSkipListSet<Element>(myComparator);
for(Element e : set)
{
   if(condition)
   {
      set.remove(e);
      set.add(anotherE);
   }
}

其中 eanotherE 通过提供的比较器相等。

最佳答案

是的,它很安全。来自 java docs :

Insertion, removal, and access operations safely execute concurrently by multiple threads. Iterators are weakly consistent, returning elements reflecting the state of the set at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other operations.

关于java - 迭代时向 ConcurrentSkipListSet 删除和添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508852/

相关文章:

Magento 集合表达式

java - 我试图根据名字做空员工,但出现此错误?

java - 如何将不同的列表分组到另一个列表中?

collections - Rust 中 Kotlin 的 `reduce` 操作的替代方案是什么?

java - “Your security settings have blocked a local application from running” Java 8

java - 什么是 Java 中的参数多态性(附示例)?

java - 发布前优化Android应用

java - JTable数据垂直滚动时不稳定

java - 在 Java Swing 中显示警告对话框

php - 将键/值对与 Laravel 模型关系隔离