6 Replies Latest reply: Mar 8, 2011 4:17 AM by 838458 RSS

    why hashtable donot allow null values

    838458
      In the source code of hashtable I found they are checking for null values and throwing null exception is there any specific reason why they are doing so?
      and also is there any relation between checking of null and synchronization?
        • 1. Re: why hashtable donot allow null values
          JoachimSauer
          user8483670 wrote:
          In the source code of hashtable I found they are checking for null values and throwing null exception is there any specific reason why they are doing so?
          null values are dangerous and you usually don't want to store a mapping to null (i.e. it's usually a sign of an error).

          If the decision to enforce that in a general-purpose data structure is a wise one is a different discussion.
          and also is there any relation between checking of null and synchronization?
          No. They are completely orthogonal.
          • 2. Re: why hashtable donot allow null values
            840374
            There is null check in the put method implementation of hashtable so it does not support null values and null keys
            • 3. Re: why hashtable donot allow null values
              JoachimSauer
              roy wrote:
              There is null check in the put method implementation of hashtable so it does not support null values and null keys
              Yes, that's the reason for the initial question: Why are those checks there?
              • 4. Re: why hashtable donot allow null values
                YoungWinston
                user8483670 wrote:
                In the source code of hashtable I found they are checking for null values and throwing null exception is there any specific reason why they are doing so?
                In order to comply with line 1 of the class's documentation; to wit:
                "Any non-null object can be used as a key or as a value."
                and also is there any relation between checking of null and synchronization?
                As Joachim said: No.
                But if your question is: "is there any correlation in the facts that Hashtable is synchronized and does not allow nulls"?
                Again, no; that has more to do with age.
                Hashtable was created before the advent of the Java Collections Framework. In general, the classes created for the JCF are not synchronized.

                Winston
                • 5. Re: why hashtable donot allow null values
                  796440
                  roy wrote:
                  There is null check in the put method implementation of hashtable so it does not support null values and null keys
                  Please stop resurrecting old(ish) threads and providing rubbish answers.
                  • 6. Re: why hashtable donot allow null values
                    838458
                    null values are dangerous and you usually don't want to store a mapping to null (i.e. it's usually a sign of an error).

                    could you please explain a scenario breifing the above statement.