2

What is the difference in the internal data structure of hashmap and hashtable that manifests itself in the following differences?

  1. Hashmap allows one null key while hashtable allows none.
  2. Hashmap does not guarantee the order to be maintained - while I have not read that for a hashtable yet.

I thought that both of them essentially are an array of 'buckets' internally.

3 Answers 3

2

While since they are both using hash value to identify the elements in it. So they can not guarantee the order. And to the difference that you mentioned, it's because that they extends different abstract classes. Dictionary is an abstract map class which the key must be non-null value. And just 1 suggestion, for this kind of question that you can find more things in JDK, don't be afraid to check it.

public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable {

public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable
Sign up to request clarification or add additional context in comments.

Comments

0

Yes internally both maintain array bucket. What I could see methods of the Hashtable class are synchronized where as HashMap methods are not.

For Reference

Comments

0

You should use HashMap and ConcurrentHashMap (thread safe) unless you are studying archeology. And please assign them to Map interface instances unless you are using methods specific to either.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.