0 votes
1 view
in Java by (7.9k points)

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?

1 Answer

0 votes
by (39.8k points)

You'll require a Map<String, String>. Classes that perform the Map interface involve (but are not restricted to):

Each is created/optimized for certain conditions (go to their respective docs for more info). HashMap is apparently 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

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...