Reputation: 1834
I have one HashMap called list
which contains a few key/values
. I've got another HashMap called modify
which contains another set of key/values
.
Basically the modify
hashmap is supposed to contain a key (or keys) that match to a key in list
, and I want to update the key in list
to be the value that matches that key in modify
.
I tried:
iter = modifyList.entrySet().iterator();
while(iter.hasNext()) {
list.replace(iter.next().getKey(), iter.next().getValue());
}
However you run in to the issue where when you call iter.next().getKey()
it obviously iterates forward so now the value that I want is no longer in next()
and is instead in current
. However I can't find any way to access what is in current
Does anyone have any suggestions on the best way to accomplish this?
Upvotes: 0
Views: 1001
Reputation: 51894
Try iterating over the key and value:
modifyList.forEach(list::replace);
Which is the same as:
modifyList.forEach((k, v) -> list.replace(k, v));
Now if you're looking to remap the keys themselves, you can use a combination of put
and remove
:
modifyList.forEach((k, v) -> list.put(v, list.remove(k)));
Upvotes: 2