-6
Map<String, String> map = new HashMap<String, String>();

I am aware about the internal working of map. if the hashcode for two keys are same collision occurs and it will store in linkedlist. but what if put that key again.

is there any check on that ? or it will simply replace the value without checking ?

to elaborate more

map.put("sparsh", "sparsh");
map.put("sparsh", "1");

it will replace value "sparsh" to "1"

map.put("sparsh", "1");
map.put("sparsh", "1");

what will happen in above case, any replacement ?

and some different context from above question

System.out.println(map.put("sparsh", "sparsh"));
System.out.println(map.put("sparsh", "1"));

it is giving me first "null" and then "sparsh"(key). what is the reason behind that, if anyone has clarity about that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
sparsh610
  • 1,552
  • 3
  • 27
  • 66
  • 1
    From javadocs of `Map.put`: *Associates the specified value with the specified key in this map (optional operation). **If the map previously contained a mapping for the key, the old value is replaced by the specified value**.* Returns: *the **previous value associated with key**, or null if there was no mapping for key.* That answers both of your questions. – ernest_k Jun 20 '19 at 17:58
  • Relevant test code: https://ideone.com/opFKj2 (Yes still returns previous value if putting in same value, not null.) – Ben Dec 30 '22 at 06:51

3 Answers3

3

The contract of put is:

  • when the key is unknown, you add a new key/value pair,
  • when the key is known, you update (overwrite) the value of an existing key/value pair.

In your case, you can't observe the difference between "putting in" the new value, or keeping the old one. Two strings with two values are always equal, thus it doesn't really matter anyway.

But then: you can easily construct an example (using a class with two fields) where two objects are equal, but yet have different values (because only one of the two fields is used within the equals() implementation) .

And therefore the new value must go into the map, even if it is equal to the one already present.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
3

From documentation of Map::put :

V put(K key, V value)

Returns: the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

What does this mean

When you add an entry to the map :

  • if the entry with that key exist then it will return the last value,
  • else it will return null as you get in your example.

Here is an example with multiple puts :

System.out.println(map.put("key", "v1")); // null
System.out.println(map.put("key", "v2")); // v1
System.out.println(map.put("key", "v3")); // v2
System.out.println(map.put("key2", "v4")); // null
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

java.util.Map.put(key, value) returns the previous value associated with the same key.

So initially, there is no value associated with the key, so the first map.put("sparsh", "sparsh") returns null.

Then, since you have key: sparsh, value: sparsh in the map, calling map.put("sparsh", "1") will return the previous value, as in "sparsh"

EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • I forgot to mention that part, glad someone else did by now, and spares me the work of editing my answer again ;-) – GhostCat Jun 20 '19 at 19:24