【发布时间】:2017-08-23 21:22:53
【问题描述】:
我有一张地图,其中地图作为值,地图集作为值(在 java 中)。我为每个人编写了一个方法来复制它们,并尽量避免出现别名,但是我的程序的行为方式,我不确定它们是否有效。
private Map<String, Set<String>> deepCopySet(Map<String, Set<String>> ruledOutCount) {
Map<String,Set<String>> copy = new HashMap<String,Set<String>>();
for(Map.Entry<String, Set<String>> entry : ruledOutCount.entrySet())
{
copy.put(entry.getKey(), new HashSet<String>(entry.getValue()));
}
return copy;
}
private Map<SG, Map<classObj, Integer>> deepCopyMap(Map<SG, Map<classObj, Integer>> classCountPerSG)
{
Map<SG,Map<classObj,Integer>> copy = new HashMap<SG,Map<classObj,Integer>>();
for(Map.Entry<SG, Map<classObj,Integer>> entry : classCountPerSG.entrySet())
{
copy.put(entry.getKey(), new HashMap<classObj,Integer>(entry.getValue()));
}
return copy;
}
classObj 和 SG 是我自己的对象。 运行这些复制方法后是否有任何别名? 谢谢。
【问题讨论】:
-
“但顺便说一下我的程序的行为方式,我不确定它们是否有效。”你希望它做什么,它会做什么?