如何在迭代按键时完成更新条目
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
}
}
}
答案 0 :(得分:2)
您可以在地图的entrySet上使用Iterator - map.entrySet()。iterator()
确保在迭代时不会修改地图,但只要你自己修改就会安全:
- 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()
答案 1 :(得分:0)
要在迭代期间修改集合,您需要使用ListIteration
接口:
http://docs.oracle.com/javase/6/docs/api/java/util/ListIterator.html