Java Utililty Methods Map Copy

List of utility methods to do Map Copy

Description

The list of methods to do Map Copy are organized into topic(s).

Method

MapcopyMap(Map oMap)
Makes a true copy of provided oMap instance.
if (oMap == null) {
    return null;
Map<K, V> cMap = new HashMap<>(oMap.size());
for (Map.Entry<K, V> ome : oMap.entrySet()) {
    V oVal = ome.getValue();
    if (oVal instanceof Map) {
        oVal = (V) copyMap((Map<K, V>) oVal);
...
HashMapcopyMap(Map map)
copy Map
HashMap<String, String> result = new HashMap<String, String>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    String value = null;
    if (entry.getValue() != null) {
        value = entry.getValue().toString();
    result.put(entry.getKey(), value);
return result;
MapcopyMap(Map m)
copy Map
if (m == null || m.size() == 0) {
    return new HashMap<T, U>();
} else {
    return new HashMap<T, U>(m);
MapcopyMap(Object object)
copy Map
if (!(object instanceof Map)) {
    return null;
Map<?, ?> map = (Map<?, ?>) object;
Map<String, Object> temp = new HashMap<>();
for (Map.Entry<?, ?> entry : map.entrySet()) {
    if (entry.getKey() instanceof String) {
        temp.put((String) entry.getKey(), entry.getValue());
...
MapcopyMapButFailOnNull(Map entries)
Copy each map entry to a new map but check that each key and value isn't null.
Map<K, V> entriesToRemap = new HashMap<>(entries.size());
entries.forEach((k, v) -> {
    if (k == null || v == null) {
        throw new NullPointerException();
    entriesToRemap.put(k, v);
});
return entriesToRemap;
...
MapcopyMapWithoutEmpties(Map original)
copy Map Without Empties
Map<Object, Object> result = new HashMap<Object, Object>();
Set<?> keys = original.keySet();
for (Object key : keys) {
    Object value = original.get(key);
    if (value == null || value.toString().length() < 1)
        continue;
    result.put(key, value);
return result;
Map>copyOf( Map> map)
Returns a copy of the Map of Maps parameter.
Map<KEY_1, Map<KEY_2, VALUE>> result = new HashMap<>();
for (Map.Entry<KEY_1, ? extends Map<KEY_2, VALUE>> entry : map.entrySet()) {
    result.put(entry.getKey(), new HashMap<>(entry.getValue()));
return result;
MapcopyOf(final Map map)
copy Of
final Map<K, V> copy = new LinkedHashMap<>();
copy.putAll(map);
return copy;
MapcopyOf(Map src)
copy Of
Map<T, K> dst = new HashMap<T, K>();
copy(src, dst);
return dst;
MapcopyOnWritePut(K k, V v, Map source)
copy On Write Put
Map<K, V> map = new IdentityHashMap<K, V>(source);
map.put(k, v);
return map;