why HashTable not allow null key and value

Answers were Sorted based on User's Feedback



why HashTable not allow null key and value..

Answer / eknath

To successfully store and retrieve objects from a hashtable,
the objects used
as keys must implement the hashCode method and the equals
method.

In a nutshell, since null isn't an object, you can't call
.equals() or .hashCode() on it, so the Hashtable can't
compute a hash to use it as a key.

HashMap is newer, and has more advanced capabilities, which
are basically just an improvement on the Hashtable
functionality. As such, when HashMap was created, it was
specifically designed to handle null values as keys and
handles them as a special case.

Specifically, the use of null as a key is handled like this
when issuing a .get(key):

(key==null ? k==null : key.equals(k))

Is This Answer Correct ?    11 Yes 1 No

why HashTable not allow null key and value..

Answer / naveen

There is null check in the put method implementation of
hashtable, so it does not support null values and null keys.


public Object put(Object key, Object value) {
// Make sure the value is not null
if (value == null) throw new NullPointerException();
}

above is HashTable put method logic implemented by Sun.

Is This Answer Correct ?    6 Yes 1 No

Post New Answer

More Core Java Interview Questions

what is difference between length and length()?

8 Answers  


How many tetrahedral voids are there in bcc?

0 Answers  


What is the final keyword in java?

1 Answers  


why an outer class cannot be declared as private?

1 Answers  


Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.

0 Answers  


Which Java operator is right associative?

3 Answers  


what is request processor?

1 Answers   Virtusa,


What is the synonym of string?

0 Answers  


Program to output as below formate: 1 2 3 4 5 6 7 8 9 10

4 Answers   Huawei,


Why we use methods in java?

0 Answers  


When is the finalize() called?

0 Answers  


Why null interfaces are used in Java?

2 Answers  


Categories