0

why HashMap contains single null value in java ?

I have asked in an Interview why HashMap allows Null Value. I found an article it describing the implementation of HashMap. but doesn't show why it only contains null. what is reason behind that, why Designer design to contains Null. From Oracle Source...

public class HashMap<K,V>  extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
but why HashMap allows only single Null value ?
NLP JAVA
  • 432
  • 1
  • 3
  • 15
  • 1
    Is there any other key which is "allowed" more than once ? ;) – Nir Alfasi Sep 03 '17 at 18:30
  • but why null, it should be blank. – NLP JAVA Sep 03 '17 at 18:31
  • what should be "blank"? – luk2302 Sep 03 '17 at 18:33
  • if you aware the implementation of hashmap it contains single null but why, it should contains 16 null as per maintain the uniqueness of hashmap. – NLP JAVA Sep 03 '17 at 18:34
  • hashmap and hashset treat null values in a special way and get hashcode of them as '0':https://stackoverflow.com/questions/21535029/what-must-be-hashcode-of-null-objects-in-java – Serge Sep 03 '17 at 18:34
  • 1
    16 null are not unique, only one is "unique". And general note the hashmap does not inherently *contain* null, it just *allows* them. – luk2302 Sep 03 '17 at 18:36
  • @luk2302 but default size of hashmap is 16, when I am creating a hashmap, then how it maintains the uniqueness for rest of element. what is default key and value for these element. – NLP JAVA Sep 03 '17 at 20:09
  • You are confusing size and capacity. The size of a new `HashMap` is zero, regardless of how many `null` entries its internal array has. For such a new map, `containsKey(null)` will return `false`, as logically, there is no `null` key, as none has been put into it. Likewise, when you allocate a list using `new ArrayList<>(1000)`, it creates an array of length 1000 behind the scenes, but still is an *empty* list until you actually add elements to it. – Holger Nov 09 '17 at 07:52

2 Answers2

0

We can speculated that HashMap allows null keys and null values because it is useful to do so. Or, from the other side, it would not bring much added value to forbid nulls.

HashMap allows for just one null key because Maps in general do not support duplicate keys.

lexicore
  • 42,748
  • 17
  • 132
  • 221
0

Hashtable (the predecessor of HashMap) didn't allow null which was a pita in cases where you needed a map where you stored key-value-pairs with null as possible value because trying to set that lead to a NullPointerException.

You ended up creating a private final static Object NULL that is set as value instead and each time you set or retrieve a value you had to check for null/the representative value and change it accordingly.

HashMap allowing null as value was highly appreciated because of this.

Lothar
  • 5,323
  • 1
  • 11
  • 27