How to copy HashMap (not shallow copy) in Java

This does need iteration unfortunately. But it’s pretty trivial with Java 8 streams:

mapCopy = map.entrySet().stream()
    .collect(Collectors.toMap(e -> e.getKey(), e -> List.copyOf(e.getValue())))

Leave a Comment