249

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?

mattumotu
  • 1,144
  • 1
  • 8
  • 31
user1849819
  • 2,549
  • 2
  • 11
  • 3
  • 28
    Well, in the spirit of focusing on the question, not the user, I'm voting to reopen. I'm sure this user has finished the course by now anyway. ;) It's a duplicate of http://stackoverflow.com/questions/1540673/java-equivalent-to-python-dictionaries, but I think the answers here are better. – Adi Inbar Mar 24 '14 at 23:17

4 Answers4

331

You'll want a Map<String, String>. Classes that implement the Map interface include (but are not limited to):

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):

Map<String, String> map = new HashMap<String, String>();
map.put("dog", "type of animal");
System.out.println(map.get("dog"));
type of animal
Timo Giese
  • 55
  • 8
arshajii
  • 118,519
  • 22
  • 219
  • 270
  • 1
    @Cruncher It also implements `Map` (check the javadoc) – arshajii Nov 24 '12 at 17:17
  • 1
    @arshajii i have a doubt we manually map all the strings in the dictionary or i can say that we manually storing the meaning of all words. So my question is, is there any way like db (dump) files of dictionary to fetch the words and their meaning/definition so we can avoid adding the meaning of each word manually – Vighanesh Gursale Aug 25 '13 at 19:11
  • @VighaneshGursale You will need to use a put method to build your data structure during exeuction. Think of a put command as reading the file from disc, and placing it in memory(ram) so you can work with it. If you have a file you want loaded in as a data structure on load auto you wouuld need to look into java spring but its complex. – Dan Ciborowski - MSFT Feb 14 '14 at 23:22
  • 5
    By the way, you actually don't need to include the second time. You can just do `Map map = new HashMap<>();` – Samuel Noyes Apr 06 '16 at 15:12
  • You forgot to mention ConcurrentHashMap which more preferable to use over Hashtable – Arsenius Oct 29 '17 at 12:45
  • To save you the lookup - "The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls." - Taken from the link in the answer - https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html – Oded Ben Dov Feb 24 '21 at 19:38
86

This creates dictionary of text (string):

Map<String, String> dictionary = new HashMap<String, String>();

you then use it as a:

dictionary.put("key", "value");
String value = dictionary.get("key");

Works but gives an error you need to keep the constructor class same as the declaration class. I know it inherits from the parent class but, unfortunately it gives an error on runtime.

Map<String, String> dictionary = new Map<String, String>();

This works properly.

HK boy
  • 1,380
  • 11
  • 16
  • 21
SmartK8
  • 2,416
  • 1
  • 27
  • 32
12

Use Map interface and an implementation like HashMap

Puyover
  • 666
  • 1
  • 7
  • 20
  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – FelixSFD Aug 16 '17 at 05:34
6

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

Afshin Moazami
  • 2,003
  • 5
  • 32
  • 54
Cruncher
  • 7,241
  • 1
  • 26
  • 62