Logo

Programming-Idioms

# 298 Copy a map
Create the map y by cloning the map x.

y is a shallow copy, not a deep copy.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
var y = Map.of(x);
y := make(map[K]V, len(x))
for k, v := range x {
	y[k] = v
}
import "maps"
y := maps.Clone(x)
import java.util.Map;
Map<K, V> y = x;
my %y = %x;
y = x.copy()
y = x.dup