在我的Java应用程序中
BlockingQueue<HashMap<Integer, double[]>> q
如何克隆它?
答案 0 :(得分:7)
基本副本:Queue
BlockingQueue<HashMap<Integer, double[]>> q; // = ...
BlockingQueue<HashMap<Integer, double[]>> copy = new LinkedBlockingDeque<>(q);
深层副本:Queue
和Map
的副本
BlockingQueue<HashMap<Integer, double[]>> q; // = ...
BlockingQueue<HashMap<Integer, double[]>> copy = new LinkedBlockingDeque<>();//or other
for(HashMap<Integer, double[]> map : q){
copy.add(new HashMap<>(map));
}
非常深的副本:Queue
,Map
和double[]
的副本
BlockingQueue<HashMap<Integer, double[]>> q; // = ...
BlockingQueue<HashMap<Integer, double[]>> copy = new LinkedBlockingDeque<>();//or other
for(HashMap<Integer, double[]> map : q){
Map<Integer, double[]> mapCopy = new HashMap<>();
for(Map.Entry<Integer, double[]> entry : map.entrySet()){
double[] array = entry.getValue();
mapCopy.put(entry.getKey(), Arrays.copyOf(array, array.length));
}
copy.add(mapCopy);
}