Does hashmap and Hastable allows null key and value




Contents of page >
  • 1) Hashmap related important questions in java >
    • 1.1) Does hashmap allows null key and value ?
    • 1.2) Can a hashmap have multiple null keys and values?
  • 2) Hashtable related important questions in java >
    • 2.1) Does Hashtable allows null key and value ?

1) Hashmap related important questions in java >
1.1) Does hashmap allows null key and value ?
Yes, HashMap allows null key and null values.

1.2) Can a hashmap have multiple null keys and values?
HashMap allows to store one null key and many null values i.e. many keys can have null value in java.

Must read : HashMap and Hashtable - Similarity and Differences


2) Hashtable related important questions in java >
2.1) Does Hashtable allows null key and value ?
No, Hashtable does not allow to store null key or null value.
Any attempt to store null key or value throws runtimeException (NullPointerException) in java.


Summary >
So in this tutorial we learned that does hashmap and Hashtable allows null key and value or not?
HashMap allows to store one null key and many null values i.e. many keys can have null value in java.
Hashtable does not allow to store null key or value in java.

Map hierarchy in java

java.lang.NullPointerException


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Important Similarity and Differences in java >

COLLECTION - Top 50 interview questions and answers in java for fresher and experienced

Collection vs Collections - Differences in java

ArrayList vs LinkedList - Similarity and Differences in java


Must read for you :