2012-07-06 71 views
0

你將如何完成更新的條目,而迭代通過按鍵更新項設置

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 
     } 
    } 
} 

回答

2

您可以在地圖上的使用的entrySet一個Iterator - map.entrySet()迭代器()

馬柯確保沒有其他修改地圖,而你通過它迭代,而是你自己的修改將是安全的,只要你:

- 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()