How do you create a dictionary in Java? [closed]
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
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.