Difference between a HashMap and a dictionary ADT

JavaData Structures

Java Problem Overview


What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?

Java Solutions


Solution 1 - Java

In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)

The Dictionary class has been marked as obsolete, so it's best not to use it.

Solution 2 - Java

This Stack Overflow post does a good job explaining the key differences:

Java hashmap vs hashtable

Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".

The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.

Solution 3 - Java

In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.

You are advised to use the HashMap though.

Solution 4 - Java

Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.

Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.

There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionashokgelalView Question on Stackoverflow
Solution 1 - JavaPhilView Answer on Stackoverflow
Solution 2 - JavaJim NelsonView Answer on Stackoverflow
Solution 3 - JavaVincent RamdhanieView Answer on Stackoverflow
Solution 4 - JavamasecView Answer on Stackoverflow