Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

write a simple program inheritance?

2 Answers   Polaris,


how many ways to create Thread and which one is good? runnable interface ot Thread class?

3 Answers   Satyam,


What is difference between synchronize and concurrent collection in java?

0 Answers  


Which class represents the socket that both the client and server use to communicate with each other?

0 Answers  


Can you explain the meaning of aggregation and composition

0 Answers  


How two different class threads communicate with each other?. send example code.

4 Answers  


If we allocate the memory using 'new' & de-allocated using 'free' then what will happen?

0 Answers   CDAC,


what is the difference between static class and singleton class? can we create static class?

2 Answers   L&T, Octazen, Vamsi Labs,


What do you understand by classes in java?

0 Answers  


Is it possible to do method overloading and overriding at a time

3 Answers   L&T,


what do you mean by java annotations?

0 Answers  


Can we override constructors in java?

0 Answers  


Categories