2

Are there are differences between hashmap and hashtable in theory?

I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap?

woliveirajr
  • 9,433
  • 1
  • 39
  • 49
Brian Crafton
  • 438
  • 2
  • 6
  • 15
  • 2
    possible duplicate of [Differences between HashMap and Hashtable?](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable) – Vinayak Pingale May 07 '15 at 19:43
  • HashMap (in java) is an implementation of the hash table (the data structure). – Isaiah van der Elst May 07 '15 at 19:45
  • 1
    Is the question "what's the difference between java.util.Hashtable and java.util.HashMap?" or is the question referring to the hash table as a data structure? – Isaiah van der Elst May 07 '15 at 19:49
  • 3
    I think he asks if there are hashtables and hashmaps, as different things, in some theorical definition. Not the Java implementation of them – woliveirajr May 07 '15 at 19:52
  • sorry I went afk. Yes I meant the data structure ... or theoretical definition, not the java implementation. I saw that question, but it was regarding the java implementation not an abstract or theoretical idea. – Brian Crafton May 07 '15 at 21:06
  • See also https://stackoverflow.com/q/32274953/410767 – Tony Delroy Jan 28 '23 at 22:16

5 Answers5

3

According to Wikipedia, they are the same:

In computing, a hash table (hash map) is a data structure used to implement an associative array (...)

According to Wikibooks, it's the same:

A hash table, or a hash map, is a data structure that associates keys with values.

Some answer on StackOverflow also states:

Hashtable is often useful (they are also called hashmaps) (...)

Community
  • 1
  • 1
woliveirajr
  • 9,433
  • 1
  • 39
  • 49
  • Note that what you've quoted from WIkipedia talks about "hash table (hash map)", which is a ambiguous: a parenthesised term like that can be used to notate a synonymous name, but it can also be used to limit the scope to a subcategory. For example, "the politicians (Australian) are often corrupt". It's clumsy wording, but the Wikipedia quote is only correct if interpreted in the latter way. The wikibooks quote is just wrong, but it's reasonable to list it here. You'll find lots of answers on stackoverflow saying different things, so there's not much point picking something at random.... – Tony Delroy Jan 28 '23 at 22:28
0

Hash Map & Hashtable

  • Both belongs to Map Interface. Keys – cannot be duplicate.
  • Hash Map  not synchronized but we can convert it to synchronizedMap. It allows maximum one null key and any number of null values.
  • Hash Table  synchronized as it is legacy class. It not allow null either in key/value.
  • The default initial capacity of Hashtable is 11, HashMap is 16.

  • Hash – will not ensure the order of insertion

  • For More information see my stack post

public static void main(String[] args) {
    new Thread() {
        @Override public void run() {
            HashMap<String, Integer> hm = new HashMap<String, Integer>();
            hm.put("key0", 10); // Compiler Widens.
            hm.put("key1", null);
            hm.put("key0", new Integer(16)); // Overridden.
            hm.put(null, 20);
            hm.put(null, 70);
            hm.put(null, null);
            System.out.println("HashMap : "+hm); // hm.toString()

            Iterator<Entry<String, Integer>> it = hm.entrySet().iterator();
            while (it.hasNext()) {                  
                Map.Entry<String, Integer> pair = (Map.Entry<String, Integer>)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                it.remove(); // avoids a ConcurrentModificationException
            }
            // we can conver HashMap to synchronizedMap
            Collections.synchronizedMap(hm);

        }
    }.start();
    new Thread() {
        @Override public void run() {
            Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
            try {
                ht.put("product1", 12);
                ht.put("product2", 13);
                ht.put("product2", 14);
            //  ht.put(null, 20);
            //  ht.put("product2", null);
                System.out.println("hash Table : "+ht);

            } catch (NullPointerException nul) {
                System.out.println("HashTable will not accept null's eighter in key/value");
                IllegalArgumentException iae = new IllegalArgumentException("nulls not accept");
                iae.initCause(nul);
                throw iae;
            }               
        }
    }.start();
}

Hash Map & Hashtable

Yash
  • 9,250
  • 2
  • 69
  • 74
0
  1. Synchronization or Thread Safe :This is the most important difference between two . HashMap is non synchronized and not thread safe.On the other hand, HashTable is thread safe and synchronized.HashMap should be used if your application do not require any multi-threading task, in other words hashmap is better for non-threading applications. HashTable should be used in multithreading applications.

  2. Null keys and null values :Hashmap allows one null key and any number of null values, while Hashtable do not allow null keys and null values in the HashTable object.

  3. Performance :Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.

  4. HashMap inherits AbstractMap class while Hashtable inherits Dictionary class.

  5. The significant difference between HashMap vs Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort. This is also an important difference between Enumeration and Iterator in Java.

Freeze Francis
  • 517
  • 8
  • 18
0

the main difference is that the HashMap is accessed by all the threads at the same time and most of them do some changes, as a result of that some changes wont be visible for every single thread ( async) . The hashTable is sync and only one thread can access and change the the HashTable.

Hilal Aissani
  • 709
  • 8
  • 3
0

Isn't a hashtable a map that uses hashing ... hence a hashmap?

Yes, exactly. In Computing Science, a map is a functional requirement for an associative container: the ability to map from values to keys. A hash table is an implementation technique accessing an array of buckets using an index returned by invoking a hash function on a key. They can and often are used together, but you can have a "map" based on say a balanced binary tree, and you can have a hash table that stores a set of value (such that the keys are the values, and there is no "map"ping).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252