Java排序映射更改键

Java排序映射更改键,java,java-8,java-stream,Java,Java 8,Java Stream,我有下面的测试应用程序。我的目标是在SortMap中更改所有键。 在下面的代码中,我得到了一个java.util.ConcurrentModificationException 我现在的问题是如何做到这一点。我认为使用迭代器可以很好地工作 public TestClass() { final SortedMap<String, String> sm = new TreeMap<>(); sm.put("1", "one"); sm.put("2",

我有下面的测试应用程序。我的目标是在SortMap中更改所有键。 在下面的代码中,我得到了一个
java.util.ConcurrentModificationException

我现在的问题是如何做到这一点。我认为使用迭代器可以很好地工作

public TestClass() {
    final SortedMap<String, String> sm = new TreeMap<>();
    sm.put("1", "one");
    sm.put("2", "two");
    sm.put("3", "three");
    sm.put("4", "four");
    sm.put("5", "five");

    System.out.println(sm);

    for (final Iterator<String> iterator = sm.keySet().iterator(); iterator.hasNext();) {
        final String next = iterator.next();
        sm.put(next + "-shifted", sm.remove(next));
    }

    System.out.println(sm);

}

public static void main(String[] args) {
    new TestClass();
}
publictestclass(){
最终分类图sm=新树形图();
sm.put(“1”、“1”);
sm.put(“2”、“2”);
sm.put(“3”、“3”);
sm.put(“4”、“4”);
sm.put(“5”、“5”);
系统输出打印LN(sm);
for(final Iterator Iterator=sm.keySet().Iterator();Iterator.hasNext();){
最后一个字符串next=iterator.next();
sm.put(下一个+“-移位”,sm.remove(下一个));
}
系统输出打印LN(sm);
}
公共静态void main(字符串[]args){
新的TestClass();
}

问题在于,在迭代过程中同时添加和删除元素(并且也没有通过迭代器删除)

好了,没有办法替换现有的键,所以我认为您最简单的选择是用
sm
的条目构建一个新的映射

final SortedMap<String, String> sm2 = 
    sm.entrySet()
      .stream()
      .map(e -> new AbstractMap.SimpleEntry<>(e.getKey() + "-shifted", e.getValue()))
      .collect(Collectors.toMap(Map.Entry::getKey,
                                Map.Entry::getValue, 
                                (v1, v2) -> {throw new IllegalStateException();}, 
                                TreeMap::new));
最终分拣地图sm2=
sm.entrySet()
.stream()
.map(e->new AbstractMap.SimpleEntry(e.getKey()+“-Shift”,e.getValue())
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue,
(v1,v2)->{抛出新的IllegalStateException();},
树映射::新的);

您将获得一个
ConcurrentModificationException
,因为您没有在下一行中使用
迭代器
删除方法:

sm.put(next + "-shifted", sm.remove(next)); // <-- does not call iterator.remove but sm.remove
试试这个:

final SortedMap<String, String> sm = new TreeMap<String, String>();
    sm.put("1", "one");
    sm.put("2", "two");
    sm.put("3", "three");
    sm.put("4", "four");
    sm.put("5", "five");
    System.out.println(sm);
    final Object[] arrayKeys = sm.keySet().toArray();
    for (int i = 0; i < arrayKeys.length; i++) {
        sm.put(arrayKeys[i] + "-shifted", sm.remove(arrayKeys[i]));
    }
    System.out.println(sm);
final SortedMap sm=new TreeMap();
sm.put(“1”、“1”);
sm.put(“2”、“2”);
sm.put(“3”、“3”);
sm.put(“4”、“4”);
sm.put(“5”、“5”);
系统输出打印LN(sm);
最终对象[]arrayKeys=sm.keySet().toArray();
for(int i=0;i
代码变得更好,如果您删除
map
步骤,只需将
toMap
操作的键提取器从
map.Entry::getKey
更改为
e->e.getKey()+“-shift”
@Holger True,就可以避免不必要的条目创建。。打字时没有想到:/
final SortedMap<String, String> sm = new TreeMap<String, String>();
    sm.put("1", "one");
    sm.put("2", "two");
    sm.put("3", "three");
    sm.put("4", "four");
    sm.put("5", "five");
    System.out.println(sm);
    final Object[] arrayKeys = sm.keySet().toArray();
    for (int i = 0; i < arrayKeys.length; i++) {
        sm.put(arrayKeys[i] + "-shifted", sm.remove(arrayKeys[i]));
    }
    System.out.println(sm);