java.util.ConcurrentModificationException --map

时间:2024-01-17 12:45:56
key:3-key
key:/v1.02-key
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
    public static void main(String[] args) {
final String StartFlag = "/v1.0"; Map<String, String> source = new HashMap<>();
source.put("1-key", "1-value");
source.put(StartFlag + "2-key", "2-value");
source.put("3-key", "3-value"); Iterator<Map.Entry<String, String>> iterator = source.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
String key = next.getKey();
System.out.println("key:" + key);
String value = next.getValue();
if (key.startsWith(StartFlag)) {
key = key.substring(StartFlag.length());
iterator.remove();
source.put(key, value);
}
}
System.out.println(source);
}
        URL resource = getClass().getResource("/swagger/project.json");
System.out.println("URL:" + resource.toString());
URI uri = resource.toURI();
System.out.println("uri:" + uri); Path path = Paths.get(uri);
File file = path.toFile();
System.out.println("exists:" + file.exists());