可能重复:
Java中HashMap的克隆工具
我有一个一对一的映射,如下所示:
HashMap<Integer, ArrayList<Double>> matrix;
整数是索引,ArrayList 的维度约为 50。索引的大小可能高达一百万。我希望尽快制作一个副本(包括 ArrayList 值)。
我按照以下方式操作:
public Map<Integer,ArrayList<Double>> getCloneOfMatrix(){
Map<Integer, ArrayList<Double>> newMatrix = new HashMap<Integer,ArrayList<Double>>();
for(int i=0 ; i < indexSize; i++){
ArrayList<Double> arrList = new ArrayList<Double>();
arrList=(ArrayList<Double>) matrix.get(i).clone();
newMatrix.put(i,arrList);
}
return newMatrix;
}
我发现这个计算成本很高,有没有更快的方法来做它。
arrList
分配另一个引用之前立即将其分配为new ArrayList<Double>();
是完全没有意义的。 - Pete Kirkham