copy Map to another Map - Java java.util

Java examples for java.util:Map Operation

Description

copy Map to another Map

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
    public static Map copy(Map m) {
        Map newMap = new HashMap();
        scanCopy(m, newMap);//www .  jav a  2  s .co m
        return newMap;
    }

    /*******
     * copying the properties
     */
    private static void scanCopy(Map src, Map target) {
        for (Object o : src.entrySet()) {
            Map.Entry me = (Map.Entry) o;
            Object value = me.getValue();
            Object newValue = null;
            if (value instanceof Map) {
                Map _map = new HashMap();
                scanCopy((Map) value, _map);
                newValue = _map;
            } else if (value instanceof List) {
                List _lst = new ArrayList();
                Iterator iter = ((List) value).iterator();
                while (iter.hasNext()) {
                    Object x = iter.next();
                    if (x instanceof Map) {
                        Map _smap = new HashMap();
                        scanCopy((Map) x, _smap);
                        _lst.add(_smap);
                    } else {
                        _lst.add(x);
                    }
                }
                newValue = _lst;
            } else {
                newValue = value;
            }
            target.put(me.getKey(), newValue);
        }
    }
}

Related Tutorials