Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a dictionary in Java? [closed]

People also ask

How do you create a dictionary in Java?

So for creating a dictionary in Java you can use Hashtable. This class implements a hash table, which maps keys to values and any non-null object can be used as a key or as a value. In Java hierarchy Hashtable extends Dictionary and implements Map.

Is there any dictionary in Java?

A Java dictionary is an abstract class that stores key-value pairs. Given a key, its corresponding value can be stored and retrieved as needed; thus, a dictionary is a list of key-value pairs. The Dictionary object classes are implemented in java. util .

Is HashMap same as dictionary?

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.

Is there a dictionary library in Java?

Java Dictionary API Client Library (maven project) You can integrate this library into a Java application. In the sample, using the library simplifies the request process to the API server and directly returns the result object in string form.


You'll want a Map<String, String>. Classes that implement the Map 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):

Map<String, String> map = new HashMap<String, String>();
map.put("dog", "type of animal");
System.out.println(map.get("dog"));
type of animal

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.


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