java - 迭代键集时更新条目

标签 java dictionary iterator

如何在迭代键的同时完成更新条目

Map<String,List<SObject>> Map1=new HashMap<String,List<SObject>>();
Map<String,List<SObject>> Map2=new HashMap<String,List<SObject>>();

for(String name: Map1.keyset()){
//do something

   for(SObject obj1: Map1.get(name)){
    //iterate through the list of SObjects returned for the key 

        for(SObject obj2 : Map2.get(name)){
        //iterate through the list of SObject values for the keys and update or remove the values related to the key
        }
   }
 }

最佳答案

您可以在 map 的entrySet上使用迭代器 - map.entrySet().iterator()

确保在迭代 map 时没有其他任何东西正在修改 map ,但只要您满足以下条件,您自己的修改就是安全的:

- only remove items using the iterator's remove() method, and
- only modify a value by using the Map.Entry setValue() method

参见http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#entrySet()

关于java - 迭代键集时更新条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371029/

相关文章:

java - 根据点击的链接设置请求属性

Python:从具有唯一名称的文件创建多个空字典

java - 如何检查 Iterator.next() 是否为 == null?

c++ - 如何将迭代器变量声明为私有(private)成员变量?

java - 带有列表迭代器的 ClassCastException Java

java - 为什么 Hibernate 尝试删除关联对象?

java - 如何在 netbeans 代码检查中禁用 "missing java doc"警告?

java - Kotlin - 无法创建两个具有不同列表类型参数的构造函数

Sqlite 对一列的全文搜索

c++11 - 带有映射的结构的 ostream 运算符重载