why HashTable not allow null key and value
Answers were Sorted based on User's Feedback
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 |
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 |
Difference between prefix and postfix forms of the ++operator?
Why cant we define System.out.println() inside a class directly?
Is the empty set a singleton?
what is ejb? what is the importance of ejb?
The following program is Overloading or Overriding? public class PolymorphismEx { public int sampleMethod(int a) { return a; } public String sampleMethod(int a) { return "Is it Overloading or Overriding???"; } }
4 Answers Ness Technologies, TCS,
Is there any difference between synchronized methods and synchronized statements?
Is java code slower than native code?
What’s a deadlock?
Can we define constructor in inner class?
Differentiate between postfix and prefix operators in java.
Superclass of exception
Catch(Exception e){ } in that what is Exception and purpose of that in that place exactly?