I am trying to implement a dictionary (as in the physical book). I have a list of words and their meanings.
What data structure / type does Java provide to store a list of words and their meanings as key/value pairs.
How, given a key, can I find and return the value?
This creates dictionary of text (string):
you then use it as a:
Map<String, String> dictionary = new Map<String, String>();
This works properly.
Use Map interface and an implementation like HashMap
There's an Abstract Class Dictionary
http://docs.oracle.com/javase/6/docs/api/java/util/Dictionary.html
However this requires implementation.
Java gives us a nice implementation called a Hashtable
http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html
You'll want a
Map<String, String>
. Classes that implement theMap
interface include (but are not limited to):HashMap
LinkedHashMap
Hashtable
Each is designed/optimized for certain situations (go to their respective docs for more info).
HashMap
is probably the most common; the go-to default.For example (using a
HashMap
):