Shallow copy of a Map in Java

前端 未结 3 1636
南笙
南笙 2020-12-13 05:30

As I understand it, there are a couple of ways (maybe others as well) to create a shallow copy of a Map in Java:

Map data          


        
3条回答
  •  鱼传尺愫
    2020-12-13 05:49

    Copy a map without knowing its implementation:

    static final Map shallowCopy(final Map source) throws Exception {
        final Map newMap = source.getClass().newInstance();
        newMap.putAll(source);
        return newMap;
    }
    

提交回复
热议问题