Deep clone a map. - Android java.util

Android examples for java.util:Map

Description

Deep clone a map.

Demo Code


import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.elminster.common.util.Messages.Message;

public class Main{
    /**//from w w  w. j a  v a 2s.  c om
     * Deep clone a map.
     * 
     * @param map
     *          map to clone
     * @return cloned map
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Map cloneMap(Map map) {
        Map cloned = null;
        if (CollectionUtil.isNotEmpty(map)) {
            try {
                cloned = (Map) map.getClass().newInstance();
                Set<Entry> set = map.entrySet();
                Iterator<Entry> it = set.iterator();
                while (it.hasNext()) {
                    Entry entry = it.next();
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    if (value instanceof Cloneable) {
                        Object clone;
                        // try to call clone()
                        try {
                            clone = ReflectUtil.invoke(value, "clone",
                                    new Object[] {}); // $NONNLS1$
                            // if successful
                            value = clone;
                        } catch (IllegalArgumentException e) {
                            // ignore
                        } catch (NoSuchMethodException e) {
                            // ignore
                        } catch (InvocationTargetException e) {
                            // ignore
                        } catch (Exception e) {
                            // ignore
                        }
                    }
                    cloned.put(key, value);
                }
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return cloned;
    }
    /**
     * Check whether the specified collection is not empty
     * 
     * @param collection
     *          the specified collection
     * @return whether the the specified collection is not empty
     */
    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }
    /**
     * Check whether the specified map is not empty
     * 
     * @param map
     *          the specified map
     * @return whether the the specified map is not empty
     */
    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }
    /**
     * Check whether the specified collection is empty
     * 
     * @param collection
     *          the specified collection
     * @return whether the the specified collection is empty
     */
    public static boolean isEmpty(Collection<?> collection) {
        return null == collection || collection.isEmpty();
    }
    /**
     * Check whether the specified map is empty
     * 
     * @param map
     *          the specified map
     * @return whether the the specified map is empty
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return null == map || map.isEmpty();
    }
}

Related Tutorials