Questions tagged [sortedmap]

In Java, a SortedMap is a map object that provides an ordering on its keys.

SortedMap is a Java interface for a map that further provides a total ordering on its keys. Quoting the official documentation:

The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of SortedSet.)

All keys inserted into a sorted map must implement the Comparable interface (or be accepted by the specified comparator). Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) (or comparator.compare(k1, k2)) must not throw a ClassCastException for any keys k1 and k2 in the sorted map. Attempts to violate this restriction will cause the offending method or constructor invocation to throw a ClassCastException.

Similar SortedMap interfaces exist in other languages, including Scala.

113 questions
87
votes
6 answers

Why are there no sorted containers in Python's standard libraries?

Is there a Python design decision (PEP) that precludes a sorted container from being added to Python? (OrderedDict is not a sorted container since it is ordered by insertion order.)
Neil G
  • 28,787
  • 31
  • 143
  • 234
73
votes
6 answers

How to use SortedMap interface in Java?

I have a Map What is the best way to keep the map sorted according to the float? Is SortedMap the best answer? TreeMap? How do I use it? I only create the map once and replace the MyObject frequently using myMap.put() and…
Bick
  • 15,895
  • 44
  • 133
  • 231
32
votes
3 answers

Java TreeMap Comparator

I need a comparator for a TreeMap. Should I write this anonymously in the constructor for my TreeMap? How else could I write my comparator. Currently, Java does not like my code (can I do this anonymously?): SortedMap myMap = …
CodeKingPlusPlus
  • 12,865
  • 46
  • 123
  • 204
20
votes
8 answers

Find element position in a Java TreeMap

I am working with a TreeMap of Strings TreeMap, and using it to implement a Dictionay of words. I then have a collection of files, and would like to create a representation of each file in the vector space (space of words) defined by…
Matteo
  • 6,694
  • 21
  • 75
  • 123
11
votes
1 answer

Scala: Why does SortedMap's mapValues returns a Map and not a SortedMap?

I'm new to Scala. I'm using SortedMap in my code, and I wanted to use mapValues to create a new map with some transformation on the values. Instead of returning a new SortedMap, the mapValues function returns a new Map, which I then have to convert…
Oren
  • 2,647
  • 3
  • 22
  • 37
10
votes
2 answers

Why is EnumMap not a SortedMap in Java?

EnumMap, V> in Java is clearly ordered by definition of the associated enum, as you can also see in the javadoc: Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). …
rawcode
  • 103
  • 6
9
votes
5 answers

Java "cannot cast to Comparable" when using TreeMap

Possible Duplicate: Java: SortedMap, TreeMap, Comparable? How to use? I am using the Java JungI graph package and Netbeans 7. I am getting the following error from Java: Exception in thread "main" java.lang.ClassCastException:…
CodeKingPlusPlus
  • 12,865
  • 46
  • 123
  • 204
7
votes
2 answers

TreeMap lastKey lookup time

What is the time complexity of TreeMap.lastKey() part of the SortedMap interface? The oracle docs mention this about TreeMaps: This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.
Arjun Patel
  • 236
  • 2
  • 16
7
votes
4 answers

Scala SortedMap.map method returns non-sorted map when static type is Map

I encountered some unauthorized strangeness working with Scala's SortedMap[A,B]. If I declare the reference to SortedMap[A,B] "a" to be of type Map[A,B], then map operations on "a" will produce a non-sorted map implementation. Example: import…
6
votes
1 answer

How to initialize a TreeMap with pre-sorted data?

My app uses a TreeMap to keep data sorted and have log(n) lookups & inserts. This works great in the general case while the app is running, but when the app first starts, I need to initialize the TreeMap with several million longs that I get in…
Yevgeniy Brikman
  • 7,399
  • 4
  • 38
  • 55
6
votes
1 answer

What is the time complexity of floorEntry() method of NavigableMap?

If I have a NavigableMap already formed. What will be the time required for a floorEntry() operation to execute? will it be O(1) or O(logn)? for example: If I have a NavigableMap with n intervals, and I use map.floorEntry(k) for some random k, what…
Neil
  • 61
  • 2
6
votes
2 answers

How do I form the union of scala SortedMaps?

(I'm using Scala nightlies, and see the same behaviour in 2.8.0b1 RC4. I'm a Scala newcomer.) I have two SortedMaps that I'd like to form the union of. Here's the code I'd like to use: import scala.collection._ object ViewBoundExample { class…
Scott Morrison
  • 3,090
  • 21
  • 39
6
votes
1 answer

Create SortedMap from Iterator in scala

I have an val it:Iterator[(A,B)] and I want to create a SortedMap[A,B] with the elements I get out of the Iterator. The way I do it now is: val map = SortedMap[A,B]() ++ it It works fine but feels a little bit awkward to use. I checked the…
Chirlo
  • 5,679
  • 1
  • 25
  • 42
6
votes
5 answers

Java: SortedMap, TreeMap, Comparable? How to use?

I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do…
Nick Heiner
  • 108,809
  • 177
  • 454
  • 689
5
votes
2 answers

How to create sorted map in scala?

How to create sorted map in scala (mutable/immutable)?
Jeriho
  • 6,779
  • 9
  • 39
  • 56
1
2 3 4 5 6 7 8