0

I know that getForNullKey() resolves the null key entries in a HashMap. But will there be a scenario that there are collisions at node 0 of the HashMap. If there are multiple objects whose hashcode return zero, will they be stored in the linked list at node zero? Does getForNullKey() help in obtaining the output

private V getForNullKey() {
  for (Entry<K,V> e = table[0]; e != null; e = e.next) {
      if (e.key == null)
         return e.value;
 }
 return null;
}

I am a bit confused about wht the e.value returns.

EDIT:

The doubt which I have is:Suppose we peek into a HashMap
node no 4->["Key1"|1234|"Value1"|Point to next node]--> ["Key2"|12345|"Value2"|Point to next node]

is the picture at node 4 which has collision owing to two keys that ended up in the same node.

Can such a thing happen at node zero as well ? I know how "Value2" at node 4 can be reached using get().

But can getForNullKey() help in getting to the multiple entries at node zero..if that happens. Still learning HashMap hence a curious doubt :)

VSTech
  • 77
  • 3
  • 11
  • This question is unclear... `e.value` is the value for the entry `e`, but it's not clear what that has to do with anything else. Yes, entries with a hash code of 0 will be stored in the first bucket in the table. Yes, the entry corresponding with a null key would also be stored in that bucket. – Jon Skeet Nov 14 '15 at 13:47
  • @JonSkeet: I assume I'm not the first to make [this joke](http://stackoverflow.com/questions/33573773/is-it-possible-to-trick-this-windowsidentity-code-into-using-the-wrong-user#comment54926194_33573773). ;-) – T.J. Crowder Nov 14 '15 at 13:48
  • @T.J.Crowder: Can't say I've heard that one. I'm fond of http://memegenerator.net/instance/62989260 though... – Jon Skeet Nov 14 '15 at 13:49
  • @JonSkeet: I have elaborated my question. Could you please have a look. – VSTech Nov 14 '15 at 14:11
  • It's still really not clear. Yes, there can be multiple entries in the first node of the table - which is why `getForNullKey` has a loop in it. I don't know what you mean by "can `getForNullKey` help in getting to the multiple entries at node zero" - `getForNullKey` gets the value associated with the null key, and that's *all* it does. – Jon Skeet Nov 14 '15 at 14:16
  • It would be surprising if a node (bucket) with index 0 should be different from all the rest. The fact that the null key also "hashes" into this bucket is just convenient, because that bucket is always there, if any. – laune Nov 14 '15 at 14:37
  • @JonSkeet Thanks for the confirmation. For a non zero node scenario, the check on collision is (1) Is the Hashcode of the key passed and the object in the HashMap the same (2) If hashcode same, check the objects using equals method and provide the value (3) If hashcode not same, move to the next entry pointed by the linked list. For a zero node scenario, too this will be possible. Only the hashcodes are guaranteed to be same in this case. Hence the equals method will be used to check the objects and return values. That is handled by getForNullKey. This is my understanding. – VSTech Nov 14 '15 at 14:46
  • @SusanMiller: No, the hashcodes *aren't* guaranteed to be the same. There can be non-zero hashcodes which still end up in the same bucket. `getForNullKey` only checks whether the key is `null` rather than checking the hashcode because that's all that's required - it's not like there could be entries with a null key but a different hashcode... – Jon Skeet Nov 14 '15 at 14:48
  • @JonSkeet I have read that Hashcode of null key is zero. So at least that is guaranteed. Would you agree? – VSTech Nov 14 '15 at 15:18
  • @SusanMiller: Yes, but that's not the same as what you claimed which was that entries in the zero node are guaranteed to have the same hash code. – Jon Skeet Nov 14 '15 at 15:31
  • @JonSkeet Now things are clear :) – VSTech Nov 14 '15 at 15:35

0 Answers0