2010-11-21 97 views
17

是否可以在迭代過程中更改同一個HashMap實例的鍵?因爲地圖條目集沒有方法entry.setKey()。現在我能想到的是創造另一個HashMap中......在迭代過程中更改HashMap鍵

MultipartParsingResult parsingResult = parseRequest(request); 

Map<String, String[]> mpParams = parsingResult.getMultipartParameters(); 
Map<String, String[]> mpParams2 = new HashMap<String, String[]>(); 

Iterator<Entry<String,String[]>> it = mpParams.entrySet().iterator(); 

while (it.hasNext()) { 
    Entry<String,String[]> entry = it.next(); 
    String name = entry.getKey(); 

    if (name.startsWith(portletNamespace)) { 
     mpParams2.put(name.substring(portletNamespace.length(), name.length()), entry.getValue()); 
    } 
    else { 
     mpParams2.put(name, entry.getValue()); 
    } 
} 

回答

9

你應該保持信息在其他集合迭代後對其進行修改。您只能在迭代器中使用iterator.remove()刪除條目。 HashMap合約在迭代過程中禁止變異它。

+5

您可以*永遠不會*更改地圖中的鍵值。 – EJP 2010-11-21 00:33:07

+0

因此從性能角度來看,我在這裏的解決方案是否正常?因爲每小時會有數千次這樣的電話 – lisak 2010-11-21 00:36:20

+0

@EJP我在這裏改變的只是地圖的鑰匙 – lisak 2010-11-21 00:37:18

2

您可能想要對HashMap中的鍵或值執行四種常見類型的修改。

  1. 要更改HashMap鍵,請用get查找value對象,然後刪除舊鍵並將其與新鍵一起使用。
  2. 要更改值對象中的字段,請使用get鍵向上查看value對象,然後使用其setter方法。
  3. 要完全替換其中的值對象,只需在舊鍵處添加一個新的值對象即可。
  4. 要用基於舊值的對象替換值對象,請使用get查看值對象,創建新對象,從舊數據複製數據,然後將新對象置於同一個鍵下。

就像這個例子。

static class Food 
    { 
    // ------------------------------ FIELDS ------------------------------ 

    String colour; 

    String name; 

    float caloriesPerGram; 
    // -------------------------- PUBLIC INSTANCE METHODS -------------------------- 

    public float getCaloriesPerGram() 
     { 
     return caloriesPerGram; 
     } 

    public void setCaloriesPerGram(final float caloriesPerGram) 
     { 
     this.caloriesPerGram = caloriesPerGram; 
     } 

    public String getColour() 
     { 
     return colour; 
     } 

    public void setColour(final String colour) 
     { 
     this.colour = colour; 
     } 

    public String getName() 
     { 
     return name; 
     } 

    public void setName(final String name) 
     { 
     this.name = name; 
     } 

    public String toString() 
     { 
     return name + " : " + colour + " : " + caloriesPerGram; 
     } 

    // --------------------------- CONSTRUCTORS --------------------------- 

    Food(final String name, final String colour, final float caloriesPerGram) 
     { 
     this.name = name; 
     this.colour = colour; 
     this.caloriesPerGram = caloriesPerGram; 
     } 
    } 

// --------------------------- main() method --------------------------- 

/** 
* Sample code to TEST HashMap Modifying 
* 
* @param args not used 
*/ 
public static void main(String[] args) 
    { 
    // create a new HashMap 
    HashMap<String, Food> h = new HashMap<String, Food>(149 
      /* capacity */, 
      0.75f 
      /* loadfactor */); 

    // add some Food objecs to the HashMap 
    // see http://www.calorie-charts.net for calories/gram 
    h.put("sugar", new Food("sugar", "white", 4.5f)); 
    h.put("alchol", new Food("alcohol", "clear", 7.0f)); 
    h.put("cheddar", new Food("cheddar", "orange", 4.03f)); 
    h.put("peas", new Food("peas", "green", .81f)); 
    h.put("salmon", new Food("salmon", "pink", 2.16f)); 

    // (1) modify the alcohol key to fix the spelling error in the key. 
    Food alc = h.get("alchol"); 
    h.put("alcohol", alc); 
    h.remove("alchol"); 

    // (2) modify the value object for sugar key. 
    Food sug = h.get("sugar"); 
    sug.setColour("brown"); 
    // do not need to put. 

    // (3) replace the value object for the cheddar key 
    // don't need to get the old value first. 
    h.put("cheddar", new Food("cheddar", "white", 4.02f)); 

    // (4) replace the value object for the peas key with object based on previous 
    Food peas = h.get("peas"); 
    h.put("peas", new Food(peas.getName(), peas.getColour(), peas.getCaloriesPerGram() * 1.05f)); 

    // enumerate all the keys in the HashMap in random order 
    for (String key : h.keySet()) 
     { 
     out.println(key + " = " + h.get(key).toString()); 
     } 
    }// end main 
} 

我希望這有助於

17

也許這可以幫助:

map.put(newkey,map.remove(oldkey)); 
+0

如果我遍歷鍵集,這個新添加的鍵將出現在現有的鍵集中? – Ring 2015-04-22 21:57:58

0

做的最好的事情是地圖複製到一個新的你想要的修改,然後返回這個新映射並摧毀舊的。 但我想知道這個解決方案對性能的影響是什麼。