According to this link in Java 8, to avoid collisions in map (HashMap, LinkedHashMap, and ConcurrentHashMap
) are implemented using balanced trees instead of LinkedList
.
Then what is the difference if:
- Both (
TreeMap
and Other Maps (HashMap, LinkedHashMap, and ConcurrentHashMap
) got implemented using self-balancing-trees, as worst case accessibility is same. Sorting of
entry
I can achieve as follows:public <K extends Comparable,V extends Comparable> LinkedHashMap<K,V> sortByKeys(LinkedHashMap<K,V> map){ List<K> keys = new LinkedList<K>(map.keySet()); Collections.sort(keys, (Comparator<? super K>) new Comparator<String>() { @Override public int compare(String first, String second) { return first.compareTo(second); } }); }
Apart from sorting and accessbility what are the other properties of TreeMap
?