Thursday 15 March 2012

adding a key value pair in java hashMap -



adding a key value pair in java hashMap -

i have map maps cost position:

map<vector<double>,double> positioncost=new hashmap<vector<double>,double>();

positions vectors of type double.

i putting cost each position by:

positioncost.put(position, newcost);

now have vector save cost produced cost.add(newcost); positions. there 1 problem - size of hashmap not equal size of vector of costs.

system.out.println("no of particles"+" "+cost.size()+positioncost.size());

i not able figure out why.

the size of positioncost map won't same size of cost vector if adding same position key more 1 time map. in case, latter value associated key overwrite previous value associated key, , size of map remain same.

you should add together status before adding map :

if (!positioncost.containskey(position)) { positioncost.put(position, newcost); } else { // key exists in map. might bug, or might // valid situation have decide how handle }

java hashmap

No comments:

Post a Comment