What are the differences between a HashMap and a Hashtable in Java?

后端 未结 30 2417
青春惊慌失措
青春惊慌失措 2020-11-21 13:30

What are the differences between a HashMap and a Hashtable in Java?

Which is more efficient for non-threaded applications?

相关标签:
30条回答
  • 2020-11-21 14:06

    Keep in mind that HashTable was legacy class before Java Collections Framework (JCF) was introduced and was later retrofitted to implement the Map interface. So was Vector and Stack.

    Therefore, always stay away from them in new code since there always better alternative in the JCF as others had pointed out.

    Here is the Java collection cheat sheet that you will find useful. Notice the gray block contains the legacy class HashTable,Vector and Stack.

    enter image description here

    0 讨论(0)
  • 2020-11-21 14:06

    In addition to what izb said, HashMap allows null values, whereas the Hashtable does not.

    Also note that Hashtable extends the Dictionary class, which as the Javadocs state, is obsolete and has been replaced by the Map interface.

    0 讨论(0)
  • 2020-11-21 14:07

    HashMap: An implementation of the Map interface that uses hash codes to index an array. Hashtable: Hi, 1998 called. They want their collections API back.

    Seriously though, you're better off staying away from Hashtable altogether. For single-threaded apps, you don't need the extra overhead of synchronisation. For highly concurrent apps, the paranoid synchronisation might lead to starvation, deadlocks, or unnecessary garbage collection pauses. Like Tim Howland pointed out, you might use ConcurrentHashMap instead.

    0 讨论(0)
  • 2020-11-21 14:08

    Beside all the other important aspects already mentioned here, Collections API (e.g. Map interface) is being modified all the time to conform to the "latest and greatest" additions to Java spec.

    For example, compare Java 5 Map iterating:

    for (Elem elem : map.keys()) {
      elem.doSth();
    }
    

    versus the old Hashtable approach:

    for (Enumeration en = htable.keys(); en.hasMoreElements(); ) {
      Elem elem = (Elem) en.nextElement();
      elem.doSth();
    }
    

    In Java 1.8 we are also promised to be able to construct and access HashMaps like in good old scripting languages:

    Map<String,Integer> map = { "orange" : 12, "apples" : 15 };
    map["apples"];
    

    Update: No, they won't land in 1.8... :(

    Are Project Coin's collection enhancements going to be in JDK8?

    0 讨论(0)
  • 2020-11-21 14:08

    There are 5 basic differentiations with HashTable and HashMaps.

    1. Maps allows you to iterate and retrieve keys, values, and both key-value pairs as well, Where HashTable don't have all this capability.
    2. In Hashtable there is a function contains(), which is very confusing to use. Because the meaning of contains is slightly deviating. Whether it means contains key or contains value? tough to understand. Same thing in Maps we have ContainsKey() and ContainsValue() functions, which are very easy to understand.
    3. In hashmap you can remove element while iterating, safely. where as it is not possible in hashtables.
    4. HashTables are by default synchronized, so it can be used with multiple threads easily. Where as HashMaps are not synchronized by default, so can be used with only single thread. But you can still convert HashMap to synchronized by using Collections util class's synchronizedMap(Map m) function.
    5. HashTable won't allow null keys or null values. Where as HashMap allows one null key, and multiple null values.
    0 讨论(0)
  • 2020-11-21 14:08

    HashMap: It is a class available inside java.util package and it is used to store the element in key and value format.

    Hashtable: It is a legacy class which is being recognized inside collection framework.

    0 讨论(0)
提交回复
热议问题