Android/Java Map深复制(修改复制的Map不影响原Map)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m940034240/article/details/88716957
public static <E,T> Map<E,T> deepCopy(Map<E,T> src) {
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        Map<E,T>  dest = (Map<E,T> ) in.readObject();
        return dest;
    } catch (Exception e) {
        e.printStackTrace();
        return new HashMap<E,T>();
    }
}

猜你喜欢

转载自blog.csdn.net/m940034240/article/details/88716957
今日推荐