In Hashmap for null key the index is 0 but for Empty string what will be the index. I debug it and found that it is creating a linkedlist at the 0th index and storing both value there.
So why empty string value is storing in the 0th position and if it is calculating the index using the hashmap of the empty string then what will the hashcode of empty string.
HashMap<String, String> hm= new HashMap<>();
hm.put("", "");
hm.put(null, null);
Because hashcode for an empty string returns 0, and that it is the same value for a null object. So you have a collision of hash, so it goes to the same cell.
* At least for the current implementation in the class String - that could change one day
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With