Why use sortedmap




















Here is an example of obtaining a tail map from a SortedMap via its tailMap method:. The tail map returned will contain the key, value pairs "c", "3" , "d", "4" and "e", "5" , since "c", "d" and "e" are larger than or equal to the "c" passed as parameter to tailMap. The subMap method takes two parameters which act as delimiters for what elements are included in the returned submap. The submap will include all elements which have a key that is equal to or larger than the first parameter, and smaller than the second parameter.

Here is an example of obtaining a submap from a Java SortedMap via its subMap method:. The returned submap will contain the key, value pairs "b", "2 , "c", "3" and "d", "4" since the keys "b", "c" and "d" are equal to or larger than "b", and smaller than "e". There is actually a lot more you can do with a TreeMap that is not part of the SortedMap interface, like getting a descending key set etc.

Check out the JavaDoc's for more detail about these features. Tutorials About RSS. Java Collections. Ascending vs. Descending Sort Order By default the elements are iterated in ascending order, starting with the "smallest" and moving towards the "largest".

Tail Map The SortedMap interface has a method named tailMap which returns a new Map which contains the last elements of the SortedMap according to the sort order used. More Details in the JavaDoc There is actually a lot more you can do with a TreeMap that is not part of the SortedMap interface, like getting a descending key set etc.

Tweet Jakob Jenkov. Featured Videos Sponsored Ads. Use a sorted map whenever you've got sorted data and want it in a sorted order. Especially if you are doing put calls on it at various times and want it to remain in sorted order. Now, you might be tempted to use a LinkedHashMap instead if you get all the sorted data in one go, insert it in sorted order, and then only do remove calls against it if you do any modifications. It's not wrong, and it has some advantages O 1 lookups rather than O log n lookups.

But, its rather inflexible only providing the get and put and none of the other fun method calls that SortedMap can give you. Actually, SortedMap doesn't give you that much more over Map. But there is something else SortedMap has been replaced by NavigableMap since Java 1. NavigableMap has all sorts of fun method calls that are often quite useful. So, if your code only needs to work with a Map , then your code should be given a Map and nothing more specific than that.

This way, your code will work when either a Map or a SortedMap is passed to it, and the code which uses your code will be able to pass it whichever best suits its own needs. However , if your code needs to work with a SortedMap , then you cannot somehow work around that and continue using a Map under the false pretense of keeping things simple; your code will, of course, need to be given a SortedMap. This will happen if your code needs to make use of any methods which are specific to SortedMap do not exist in Map i.

The above was fairly self-explanatory, so I suspect that the reason why you are asking the question is because you have misunderstood what SortedMap is. So, here are a few notes about your question:. SortedMap is an interface without any default methods, it does not really compile into bytecode. Now, some of the methods declared by SortedMap are also declared by Map , and the reason why these declarations were duplicated in SortedMap was probably to add new documentation to them, but many methods of SortedMap do not exist in Map.

Useful reading: What is the difference between declaration and definition in Java? No additional safety is provided since the compiler can't tell any of the differences between the two. This does not make any sense. I am not sure what you are thinking, but whatever it is, it is probably wrong. SortedMap does offer an extra non-compiler-checked post condition, namely that the iterator is sorted. SortedMap begins with a doc comment which is about 4 kilobytes long, explaining what it is and how it differs from Map.

I am not sure it is a good idea to summarize this as "does offer an extra non-compiler-checked post condition". Perhaps you should read the documentation. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. When to use a SortedMap interface? Ask Question. Asked 6 years, 8 months ago. Active 6 years, 8 months ago.

Viewed 2k times. Improve this question. The rule in programming is to use the most general interface possible -- [citation-needed] — Robert Harvey.

You're expecting way too much from the type system. This type-safe map can be defined as:. Performing Various Operations on SortedMap Since SortedMap is an interface, it can be used only with a class that implements this interface.

TreeMap is the class that implements the SortedMap interface. Adding Elements: In order to add an element to the SortedMap, we can use the put method. However, the insertion order is not retained in the TreeMap. Internally, for every element, the keys are compared and sorted in the ascending order. Changing Elements: After adding the elements if we wish to change the element, it can be done by again adding the element with the put method.

Since the elements in the SortedMap are indexed using the keys, the value of the key can be changed by simply inserting the updated value for the key for which we wish to change. Removing Element: In order to remove an element from the SortedMap, we can use the remove method. This method takes the key value and removes the mapping for the key from this SortedMap if it is present in the map.

Iterating through the SortedMap: There are multiple ways to iterate through the Map. The most famous way is to use an enhanced for loop and get the keys.

It behaves like a simple map with the exception that it stores keys in a sorted format. TreeMap uses a tree data structure for storage. Objects are stored in sorted, ascending order. But we can also store in descending order by passing a comparator.



0コメント

  • 1000 / 1000