3

Are there any Java libraries that have a sorted Map (such as TreeMap) but has the ability to replace keys? For example I'm looking for something that could replace treeMapInstance.firstKey(). Speed is reasonably important which is why I'm not one for storing values, deleting, then placing values back in a new key.

In my application I may have an object such as

TreeMap<Long, Double> foo = new TreeMap<Long, Double>();

Sometimes I would like to change a key in foo without altering the associated value.

foo.put(1l, 1.0);
foo.put(2l, 2.0);

In the above for example how could I change the key 1l to 5l efficiently?

4

1 回答 1

8

In a TreeMap, both put() and remove() are O(log n), so removal followed by addition is a very good starting point. If I were you, I'd use that and then profile the application to see where the actual bottleneck is. My money is on it being someplace else.

于 2013-10-10T15:42:49.867 回答