【发布时间】:2016-11-04 00:27:59
【问题描述】:
我想替换地图中的键。
我有以下代码:
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");
map.put("e", "5");
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
Object o = next.getValue();
//how to add new element ?
//...
iterator.remove();
}
我想用keys实现map
a1->1
b2->2
c3->3
d4->4
e5->5
如果我在循环中使用map.put(next.getKey() + next.getValue(), next.getValue());,它将导致ConcurrentModificationException。
【问题讨论】:
-
我们是否保证地图中不存在新密钥?另外,您在编写代码时是否遇到任何具体问题?
-
这是某种跟踪论坛审核的测试帐户吗?
-
@Pshemo 是的,这是有保证的
-
@pandaadb 真的吗?肯定会替换之前的value?
-
@arizzle concurrentModificationException
标签: java dictionary iterator add