Categories
collections hashmap hashtable java

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

4177

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

Which is more efficient for non-threaded applications?

4

  • 34

    HashTable is obsolete in Java 1.7 and it is recommended to use ConcurrentMap implementation

    – MissFiona

    Apr 9, 2017 at 22:10

  • 9

    @MissFiona No, ConcurrentMap is not necessary here, as the Question says “non-threaded applications” meaning threading/concurrency is not an issue.

    Dec 29, 2019 at 1:11


  • 3

    @BasilBourque, Yes, but I believe what MissFiona meant by that was something akin to HashTable has traditionally been only chosen because of its partial threading protection. But that’s been obviated by ConcurrentHashMap, so it’s generally regarded as retired. It’s generally recommended to choose between HashMap or ConcurrentHashMap.” And I believe that to be a sensible comment, if it’s what she meant.

    – alife

    Feb 27 at 17:26


  • According to doi.org/10.48550/arXiv.1602.00984 Hashtable is more efficient than HashMap in terms of both energy consumption and execution time.

    – aventurin

    Jun 29 at 19:36

4131

There are several differences between HashMap and Hashtable in Java:

  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

  3. One of HashMap’s subclasses is LinkedHashMap, so in the event that you’d want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn’t be as easy if you were using Hashtable.

Since synchronization is not an issue for you, I’d recommend HashMap. If synchronization becomes an issue, you may also look at ConcurrentHashMap.

7

  • 122

    If you want to make a HashMap thread-safe, use Collections.synchronizedMap().

    Nov 22, 2011 at 18:48

  • 334

    I would also comment that the naive approach to thread-safety in Hashtable (“synchronizing every method should take care of any concurrency problems!”) makes it very much worse for threaded applications. You’re better off externally synchronizing a HashMap (and thinking about the consequences), or using a ConcurrentMap implementation (and exploiting its extended API for concurrency). Bottom line: the only reason to use Hashtable is when a legacy API (from ca. 1996) requires it.

    – erickson

    Mar 16, 2012 at 17:19

  • 8

    HashMap gives flexibility to programmer to write threadSafe code when they actually use it. It happened rarely that I needed a thread safe collection like ConcurrentHashMap or HashTable. What I needed is certain set of functions or certain statements in a synchronized block to be threadsafe.

    Jun 27, 2016 at 9:00


  • 5

    Hashtable is obsolete and we are using HashMap for non thread safe environment. If you need thread safety then you can use Collections.synchronizedMap() or use ConcurrentHashMap which is more efficient that hashtable.

    Mar 30, 2018 at 3:45

  • 4

    It’s obsolete but not deprecated and I’m wondering why this is. I’m guessing removing this class (and Vector for the same reasons) would break too much existing code and annotating with @Deprecated would imply an intention to remove the code, which apparently is not there.

    May 19, 2018 at 8:11

766

Note, that a lot of the answers state that Hashtable is synchronized. In practice this buys you very little. The synchronization is on the accessor/mutator methods will stop two threads adding or removing from the map concurrently, but in the real world, you will often need additional synchronization.

A very common idiom is to “check then put” — i.e. look for an entry in the Map, and add it if it does not already exist. This is not in any way an atomic operation whether you use Hashtable or HashMap.

An equivalently synchronised HashMap can be obtained by:

Collections.synchronizedMap(myMap);

But to correctly implement this logic you need additional synchronisation of the form:

synchronized(myMap) {
    if (!myMap.containsKey("tomato"))
        myMap.put("tomato", "red");
}

Even iterating over a Hashtable‘s entries (or a HashMap obtained by Collections.synchronizedMap) is not thread-safe unless you also guard the Map against being modified through additional synchronization.

Implementations of the ConcurrentMap interface (for example ConcurrentHashMap) solve some of this by including thread safe check-then-act semantics such as:

ConcurrentMap.putIfAbsent(key, value);

4

  • 58

    Also note that if a HashMap is modified, iterators pointing to it are rendered invalid.

    – Chris K

    Apr 22, 2009 at 22:03

  • 3

    So is there any difference between synchronized(myMap) {…} and ConcurrentHashMap in terms of thread safe?

    – telebog

    Nov 11, 2011 at 16:48


  • Having been in the middle of a JVM development crew for a number of years I can state that Hashtable’s internal synchronization is at least useful for properly pointing the finger at the customer’s code when he writes dodgy concurrent code. We received several complaints of failures inside HashMap (and hence “obviously” a JDK/JVM bug), when the cause was concurrent modification.

    – Hot Licks

    Aug 20, 2014 at 15:34

  • The implementation of Collections.synchronizedMap includes a synchronized putIfAbsent, so you don’t need to use containsKey/put yourself.

    – A248

    Jan 7, 2021 at 16:55

411

Hashtable is considered legacy code. There’s nothing about Hashtable that can’t be done using HashMap or derivations of HashMap, so for new code, I don’t see any justification for going back to Hashtable.

3

  • 109

    From Hashtable javadoc (emphasis added): “As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework.” However, you are right that it is legacy code. All the benefits of synchronization can be obtained more efficiently with Collections.synchronizedMap(HashMap). (Similar to Vector being a legacy version of Collections.synchronizedList(ArrayList).)

    – Kip

    Jan 19, 2010 at 22:09

  • 17

    @aberrant80: unfortunately you have no choice between the two and have to use Hashtable when programming for J2ME…

    – pwes

    Jan 12, 2012 at 8:13

  • Agree that “Hashtable is considered legacy code”. You should use ConcurentHashMap instead if you need concurrency.

    Jun 17, 2019 at 9:54