在迭代期间更改HashMap键

是否可以在迭代期间更改同一个HashMap实例的键? 因为映射条目集没有方法entry.setKey()。 现在我能想到的是创建另一个HashMap …

MultipartParsingResult parsingResult = parseRequest(request); Map mpParams = parsingResult.getMultipartParameters(); Map mpParams2 = new HashMap(); Iterator<Entry> it = mpParams.entrySet().iterator(); while (it.hasNext()) { Entry 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()); } } 

您应该将信息保存在其他集合中,以便在迭代后对其进行修改。 您只能在迭代器期间使用iterator.remove()删除条目。 HashMap合同禁止在迭代期间对其进行变更。

也许这有助于:

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

您可能希望对HashMap中的键或值进行四种常见的修改。

  1. 要更改HashMap密钥,请使用get查找值对象,然后删除旧密钥并将其与新密钥一起使用。
  2. 要更改值对象中的字段,请使用get键查看值对象,然后使用其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 h = new HashMap( 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 } 

我希望这有帮助

最好的办法是将地图复制到一个具有所需修改的新地图,然后返回这些新地图并销毁旧地图。 我想知道这个解决方案对性能有何影响。