dimanche 8 mars 2015

Create a hashmap dynamically based on a "template" hashmap with clean code

I have a set of data part of hash tables as follows:



static {
A_map.put(KEY_1, value0);
A_map.put(KEY_2, value1);
A_map.put(KEY_3, value2);
A_map.put(KEY_4, value3);
...
A_map.put(KEY_N, valueN);

B_map.put(KEY_1, Xvalue0);
B_map.put(KEY_2, Xvalue1);
B_map.put(KEY_3, Xvalue2);
B_map.put(KEY_4, Xvalue3);
...
B_map.put(KEY_N, XvalueN);


C_map.put(KEY_1, Yvalue0);
C_map.put(KEY_2, Yvalue1);
C_map.put(KEY_3, Yvalue2);
C_map.put(KEY_4, Yvalue3);
...
C_map.put(KEY_N, YvalueN);

....etc
}


I.e. the keys are the same in all hashes but the values are different per hash map.

And I choose which hash map to use at runtime as I have put these hashmaps as values to another hashmap.


But I think this can be more clean. I mean the keys are the same in all hashmaps and only the values should be different based on the hashmap type I choose at run time.

What is a good way to design this cleanly?


Aucun commentaire:

Enregistrer un commentaire