How to create a dictionary in java [closed]

Tags: ,



Can i get some help on creating a dictionary in java? I tried to create it as

Dictionary<String, Integer> dict = new Dictionary<String, Integer>(); 

but I am not able to run it because the ide does not show what went wrong, but greys out the run button.

Answer

You should use the java API documentation for this.

By using that, or some basic web searching, you’d find that java has the concept of a ‘dictionary’, but it is called a Map. There are various implementations with different properties. For example, if you want it to sort itself continuously on the string key:

Map<String, Integer> dict = new TreeMap<>();

The most basic version is:

Map<String, Integer> dict = new HashMap<>();

NB: Yeah, there is java.util.Dictionary. However, that’s why those docs are so important: The Dictionary doc clearly states:

NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.

So, don’t try to use it.



Source: stackoverflow