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

Where local and global variables are stored?

0 Answers  


write SQL command for table employee where print first name or last name start like "A" and who is working in domain(angular js,java,dotnet)

1 Answers  


What modifiers may be used with an inner class that is a member of an outer class in java programming?

0 Answers  


How do you define a method?

0 Answers  


Howmany address lines are required to addressing 1 MB memory?

8 Answers   Beatroot Technologies, CSC, HCL, Hexaware, IBM,






What is java class writing rules?

1 Answers   Oracle,


What is type casting?

2 Answers  


When would you use a static class?

0 Answers  


What are the restrictions that are applied to the java static methods?

0 Answers  


What if static is removed from main method?

0 Answers  


What is the major advantage of external iteration over internal iteration?

0 Answers  


Difference between Preemptive scheduling vs. Time slicing?

0 Answers  


Categories