I am storing some data in a hash map. Now I want to modify the values associated with a key based on a user input and store them these way permanently.
To make myself more clear, I have a hashmap like this:
public static HashMap<String,Integer> mymap= new HashMap<String,Integer>();
mymap.put("Hi",2);
mymap.put("Hello",3);
I will take feedback from user in some user and if he wants then I will, say, store 4 against Hello. I want these changes to be saved for future references.
I have heard about Reflection API in Java, but am not sure whether that will serve the purpose.
Reflection API allows one to manipulate/access data that is not accessable otherwise – or some data on the class that is unknown at compile time.
In here, it is really not needed. All you need is to
put()the element into the map, it will “remove” the old value from the key you just inserted (if it is already there) and associate it (the key) with the newly added value.So, basically – all you need to do is
myMap.put(key,newValue), and the implementation of theMap(assuming it is a correct one, of course) will take care of the rest.If you want to store the data between runs of the program – you will have to save it (the map) on disk. In order to do so, you can use serialization, or if you can use
Propertiesin some cases.Make sure that you load the map from disk once the program starts, or you will not see the values you stored.