如何在 Java 中克隆 BlockingQueue?

How to clone a BlockingQueue in Java?

在我的 Java 申请中

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

如何克隆它?

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