Let's say I have a hashmap. HashMap<String,String> hm = new HashMap<String,String>(); Now I put some values in this map as below. hm.put("A","First"); hm.put("B","First");
After this I again put some values for the already stored key "A".
hm.put("A","Second");
Now, if I try to get the value of "A" , I'll get "Second" as it's value.
sysout(hm.get("A"));
Output Second
Is there any way to get previous value i.e. "First" ?
Help is much appreciated.
put. If it'snull, there was nothing mapped to that key. If the result is not null, there you have the previous value associated with the key.putstatements, i.e.hm.put("A","First"); hm.put("B","First");? Is there any relationship between those two mappings and the desire to get"First"as result? Do you want to get it because"A"formerly mapped to it or because"B"has been associated with it beforehm.put("A","Second");? Or is the whole thing a trick question about the actual meaning of “previous”?