如何在Java中克隆BlockingQueue?

时间:2018-07-05 11:58:37

标签: java java-8 queue clone

在我的Java应用程序中

BlockingQueue<HashMap<Integer, double[]>> q

如何克隆它?

1 个答案:

答案 0 :(得分:7)

  1. 基本副本Queue

    的副本
    BlockingQueue<HashMap<Integer, double[]>> q; // = ...
    BlockingQueue<HashMap<Integer, double[]>> copy = new LinkedBlockingDeque<>(q);
    

  1. 深层副本QueueMap的副本

    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));
    }
    

  1. 非常深的副本QueueMapdouble[]的副本

    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);
    }