If you are using the TreeMap
to maintain an index for your values, i.e. you are using it primarily to quickly find the matching value for a given key, another thing you can do is keep 2 data structures:
- The
TreeMap
that you are using now for the indexing
- A
PriorityQueue
(or other sorted list) to iterate over your values in sorted order
Then, simply add and remove values to both lists when you have any changes. For this, you will not need to keep two copies of the values around either. You can simply add the one copy you have now to both lists, since the lists only work with the references to your values.