Can anyone help me to figure out difference between SortedMap, CheckedSortedMap, synchronizedSortedMap. I am really have trouble in making decision which one to use when?
Thanks in advance.
Can anyone help me to figure out difference between SortedMap, CheckedSortedMap, synchronizedSortedMap. I am really have trouble in making decision which one to use when?
Thanks in advance.
SortedMap
is an interface that is a Map(key-value pairs) with an added contract of being ordered in some defined manner on its keys. Owing to that ordering of keys SortedMap has extra methods like SortedMap<K,V> subMap(K fromKey, K toKey)
, SortedMap<K,V> headMap(K toKey) ...
which 'normal' Maps dont have. If you iterate a keyset of a sortedMap you will find a definite order.
A Treemap
is an implementation of that interface. If you are creating a new SortedMap object by yourself, its almost always go for TreeSet
.
For checkedSortedMap
or any other Collections.checkedXXX()
methods, Here is a fine discussion on what they all are for: What is the Collections.checkedList() call for in java?
Classes in Collections framework are not synchronized by default. For Maps it means that if you have a map that is shared between two or more threads and one is doing a put(A, value)
and another thread is also calling put(A, othervalue)
at the same time on the map, strange things can occur. Collections.synchronizedSortedMap(sortedMap)
method gives a synchronized map that wraps the given map and with proper locking mechanisms in place.
SortedMap
is an interface, which is implemented by ConcurrentSkipListMap, TreeMap
classes.
Use Collections.synchronizedSortedMap
to get thread safe (synchronized) view of sorted map. Useful in multithreaded environment.
Use java.util.Collections.checkedSortedMap
to get a dynamically typesafe view of the specified sorted map.