0

I have some questions about Java Collection objects...

  1. When we add objects to a collection like HashSet or HashMap, how the the objects internally stored?
  2. Why doesn't Hashtable allow null values?
wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Duplicate of your second question: http://stackoverflow.com/questions/7556357/why-does-hashtable-not-take-null-key – assylias Jun 28 '12 at 16:46

2 Answers2

6

You're not adding an object to the collection. You're adding a reference.

As for why HashTable doesn't allow null keys and values - it's just a design decision; in some cases it's helpful to disallow null keys, while in others it's annoying. HashMap does allow for both null keys and values, for example. There are various reasons for prohibiting nulls:

  • Usually a null key or a null value indicates a programming error in the calling code. Rejecting this at the point of insertion makes it far easier to find the error than waiting until the code fetches a value and then has an unexpected null.

  • If you know that values in a map can't be null, then you don't need to do a separate check for containment and then fetch: you can fetch, and if the result is null, you know the key was missing.

  • It takes a bit more work to handle null keys in the map implementation. While null values can sometimes be useful, null keys almost never are.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Just to be clear, a popular reason for disallowing null keys or values is because they frequently represent a logic error in the program that's using them. For example, Guava did a study on Google's code base that suggested that 95% of collections never have nulls (and rejecting nulls helps those users debug), and 5% of collections need nulls (and there's usually a workaround for that case). – Louis Wasserman Jun 28 '12 at 16:46
  • Would it be fair to say that null key is singular, since only one reference may be mapped to null at any given time? – corsiKa Jun 28 '12 at 16:48
  • Eh? You can have as many references to `null` as you like. Of course, all nulls are equal and everything... – Louis Wasserman Jun 28 '12 at 16:51
  • @LouisWasserman if a map supports null, it will be capable of supplying at most one different result for `map.get(null)` until the next call of `map.put(null,value)`. Hence my statement that there is only one reference (value) mapped to null (key) at any given time. – corsiKa Jun 28 '12 at 19:05
  • @corsiKa: Sure - although I'm not sure how helpful that is. Any particular reason for mentioning it? – Jon Skeet Jun 28 '12 at 19:07
  • @JonSkeet Perhaps it's just me, but `HashMap does allow for both null keys and values` seems to imply that the relationship of the map to null values (which a map that supports them can hold many) is the same as the relationship of the map to the null key. – corsiKa Jun 28 '12 at 19:34
  • Eh? I'm...not sure what you're saying there. Certainly a map can have at most one null key and arbitrarily many null values. – Louis Wasserman Jun 28 '12 at 19:59
0

Hashtable does not allow null values because it uses the equals and hashcode methods of the objects added to it

http://en.wikipedia.org/wiki/Hash_function

tofarr
  • 7,682
  • 5
  • 22
  • 30