遍历中修改HashMap的Key


    public static void main(String[] args) {
        HashMap hashMap = new HashMap<>();
        for (int i = 0; i < 6; i++) {
            hashMap.put("key-" + i, "value-" + i);
        }
        printMap(hashMap);
        HashMap newMap = new HashMap<>();
        Iterator> iterator = hashMap.entrySet().iterator();
        Map.Entry entry;
        while (iterator.hasNext()) {
            entry = iterator.next();
            // 往newMap中放入新的Entry
            newMap.put("prefix-" + entry.getKey(), entry.getValue());
            // 删除老的Entry 
            iterator.remove();
        }
        System.out.println("结果:");
        printMap(newMap);
    }

https://blog.csdn.net/dustin_cds/article/details/79676286

你可能感兴趣的:(Java)