is it possible to change keys of a the same HashMap instance during iteration ? Because map entry set don't have a method entry.setKey(). Now what I can think off is create another 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());
}
}
Maybe this helps:
The best thing to do is to copy the map into a new one with the modifications you want, then return this new maps and destroy the old one. I wonder what's the performance impact of this solution however.
There are four common types of modification you might want to do to the keys or values in a HashMap.
Something like this example.
I hope this helps
You should keep information in other collection to modify it after iteration. You can only remove entry using
iterator.remove()
during iterator.HashMap
contract forbids mutating it during iteration.i got to this thread when i needed to change the keys of the map entries. in my case i have a JSON representation in a Map , meaning it can hold map or list of maps, here is the code: