1

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.

4
  • 1
    The value gets overwritten. It's not possible to get previous value. You'll have to implement your own logic to capture it. Commented Dec 4, 2018 at 12:48
  • I have been asked the same question and I have replied with this same answer that it is not possible as the value gets overwritten. But the interviewer said no there is a way to get the previous value. Commented Dec 4, 2018 at 12:51
  • Use the result returned by put. If it's null, there was nothing mapped to that key. If the result is not null, there you have the previous value associated with the key. Commented Dec 4, 2018 at 16:02
  • Why does your example have two initial put statements, 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 before hm.put("A","Second");? Or is the whole thing a trick question about the actual meaning of “previous”? Commented Dec 5, 2018 at 14:31

5 Answers 5

3

No, you can however use a list as value. So it becomes:

Map<String, List<String>> map = ...; 

And then you can add or remove elements from the list to be able to retrieve previous values.

map.get(key).add(value); 

This will add to the list in the map and when searching the most recent value, you can use

List<String> list = map.get(key); String value = list.get(list.size()-1); 
Sign up to request clarification or add additional context in comments.

Comments

1

to get the previous value you can assign the return value of put method to string

hm.put("A","First"); String temp = hm.put("A","Second"); System.out.println(temp); // prints first 

Comments

1

As far as I know you could do it in 2 ways :

  1. Check if the value returned by the old key returns you a value (i.e null or not). Now you can capture that value before putting a new value to the said key.

    An illustration :

    HashMap<String, String> hmap = new HashMap<>(); hmap.put("A", "First"); if (hmap.get("A") != null) { String oldValue = hmap.get("A"); hmap.put("A", "Second"); } 
  2. We know that the put() method returns the value of the previous key

    Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

    @return the previous value associated with key

    So you could do :

    String oldValue = hmap.put("A", "Second"); 

7 Comments

Thanks for your answer, but I want to know whether we can fetch the previous value that is currently altered or not as somebody told me that there is a way to get the previous value. Now my bad is I didn't get opportunity to ask him how. :(
fetch the previous value that is currently altered -- What do you mean?
Any way that hmap.get("A") gives me "First" after alteration (hm.put("A","Second");)
Yeah. In the above answer you are getting the previous value i.e First and then it gets replaced with Second. I suggest you to try it out.
This is how I'd do it - either a println statement as mentioned or maybe store it elsewhere in some other List etc. So +1 from me.
|
0

Well there are many Opportunities to achieve this, the question is how deep do you want save the values. Do you need also the first value after 10 put operations? Do you need it sorted?

You can achieve this in an unsorted way via a HashTable , see Java HashTable: https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html

Hashtable<Key, Element> table; 

A sorted way could be, using a Stack or something :

HashMap <Key,Stack<Element>> values; 

https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html

So you can access the first element like this

values.get("A").peek(); 

If you have to access random Elements in the Stack, use LinkedList for this:

HashMap<Key,LinkedList<Element>> values; values.get("A").getFirst(); values.get("A").getLast(); 

Be sure that you override always the hashCode() and equals() Method, if you use something like HashMap's.

Comments

0

A normal map does not work that way. In a normal map the old value is overwritten and gone.

There is however a map-like data structure which saves a collection of values that has been put into the map: the multi-map.

Example:

ListMultimap<String, String> m = ArrayListMultimap.create(); m.put("A", "First"); m.put("A", "Second"); for (String s : m.get("A")) { System.out.println(s); } // Prints: // First // Second 

Multi-maps are very convenient to work with in a lot of situations, and they are well-worth learning about.

The example code here uses a multi-map from the Guava library. It can be downloaded from the Maven Repository.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.