如何在迭代键的同时完成更新条目
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/