Creating Dictionary in java?

dictionary, hashmap, java, treemap

Solution

If you need the map sorted by its keys, then use `TreeMap`, which "...provides guaranteed log(n) time cost for the containsKey, get, put and remove operations." If not, use the more general `HashMap` (which "...provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets..."), or one of the other `Map` implementations, depending on your need.

Problem

Everywhere on net, here is the way ``` Map<String, String> map = new HashMap<String, String>(); map.put("dog", "type of animal"); System.out.println(map.get("dog")); ``` My point is should it not be Treemap considering dictionary has to be sorted? Agreed lookup wont be optimized in case of Treemap but considering sorting its best data structure UPDATE :- one more requirement is return the lexicographically nearest word if the word searched is not present . I am not sure how to achieve it?

Original source