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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
SortedMapis 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 likeSortedMap<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
Treemapis an implementation of that interface. If you are creating a new SortedMap object by yourself, its almost always go forTreeSet.For
checkedSortedMapor any otherCollections.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 callingput(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.