3

Possible Duplicate:
clone utility for HashMap in java

I have an one to one map as:

      HashMap<Integer, ArrayList<Double>> matrix;

Integer is index and ArrayList has dimnesion of about 50. Index may have size upto one million. I would like to make copy(including Arraylist value) of it as soon as possible.

I did following:

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

I found it computationally expensive, is there any way to do it in faster way.

Community
  • 1
  • 1
thetna
  • 6,903
  • 26
  • 79
  • 113
  • Probably not more than a few %, but the `new ArrayList();` you assign to `arrList` immediately before assigning another reference to it is completely pointless. – Pete Kirkham Jul 18 '12 at 11:43
  • 3
    strange to have a map acting like a list... as you are iterating over the map with a for [0..indexSize], couldn't you use a list instead? – Francisco Spaeth Jul 18 '12 at 11:45
  • i don't understandt what do you mean. Can you please elaborate it? – thetna Jul 18 '12 at 11:45
  • 2
    There are some build in libraries for sparsed matrix (Colt is one example), I believe it might save you some implementation time using these – amit Jul 18 '12 at 11:46
  • It's always going to be computationally expensive to do a deep copy of an enormous Map like that. I'm not sure a Map is the right way to go at all if you're expecting that quantity of data. – Vala Jul 18 '12 at 11:46
  • Try out the answer to this question: [clone utility for hashmap][1] [1]: http://stackoverflow.com/questions/998376/clone-utility-for-hashmap-in-java – Adam Arold Jul 18 '12 at 11:48

5 Answers5

4

The fastest way to do this is to avoid needing to make the copy in the first place.

If you use a copy on write approach, you can have two references to the same structure but neither will see the others changes. This avoids the need to copy everything and depending on your usage, avoids the need to copy anything.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    This is an insightful answer, but could be more helpful if it was explained how to implement copy-on-write – L. Blanc Jun 17 '16 at 20:05
3

You are using Map.get() in every iteration, it invokes in its turn a hashCode() function, that might or might not be trivial, and then a search in the map data base.

You could simply iterate on the Entry set that is held in the Map, it will reduce the total time consumed considerably (given the fact that the ArrayList is relatively small).

for(Entry<Integer,ArrayList<Double> entry : matrix.entrySet()) {
        //get the key using entry.getKey()
        //get the value (the ArrayList) using entry.getValue()
}     

Also - you might want to consider using a library for handling mathematical matrices. Colt for example offers implementation for both dense matrix and both sparsed matrix, and it is already implemented and tested for you.

amit
  • 175,853
  • 27
  • 231
  • 333
0

Possibly cloning via serialization will help.

Viktor Stolbin
  • 2,899
  • 4
  • 32
  • 53
0

Although it won't help in terms of complexity, an ArrayList<Double> in Java requires boxing of every element, whereas - since it appears to be a sparse matrix so the second dimension is fixed - using double[] instead would give a lower cost per unit.

Other than than, use one of the classic sparse matrix representations ( assuming it is sparse or you wouldn't bother with a hash map ).

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
0

You should try out cloning with serialization / deserialization as the answer of this question states.

Community
  • 1
  • 1
Adam Arold
  • 29,285
  • 22
  • 112
  • 207