Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top