naggarwal11
naggarwal11

Reputation: 132

Is it possible to get previous stored value in Hashmap?

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.

Upvotes: 1

Views: 3204

Answers (5)

Lii
Lii

Reputation: 12122

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.

Upvotes: 0

user8439161
user8439161

Reputation:

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.

Upvotes: 0

Nicholas K
Nicholas K

Reputation: 15423

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");
    

Upvotes: 1

Eritrean
Eritrean

Reputation: 16498

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

Upvotes: 1

SBylemans
SBylemans

Reputation: 1764

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);

Upvotes: 3

Related Questions