How can I update values in a map?
I am using a TreeMap<Integer,Object>() to store values. Now my Object has a component, Object.value(), which keeps getting incremented as per values read from a file. So, I evaluate whether the key exists and need to update value. I do not understand how I can update values in a Map in Java. I cannot just replace the whole record, as new values need to be added to the existing record value. Is there a better way to do this than using a map? I used a map because I will keep searching for the keys. Any suggestions?
Well using a map is correct if you want to be able to quickly access your key-value pairs. If you're values are just MyObjects with a .value(), can't you get the object and reset that value?
MyObject myObj = treeMap.get(key); myObj.setValue(myObj.getValue()++);
I'm using MyObject here as the poster was using Object to denote an example type.