问题描述
我在C ++中有以下容器:
I have following container in C++:
std::unordered_map<keyType, std::map<otherKeyType, keyValue>::iterator> Container;
在我的代码中,我使用这个容器来快速访问std :: map容器中的元素,可以同时修改这个地图没有任何问题,总是我的迭代器是有效的(我从这个地图中只删除一个地方的元素)。
Somewhere in my code I use this container to quickly access elements in std::map container, and I can concurrently modify this map without any problems and always my iterators are valid (I remove elements from this map only in one place).
我想重现这种行为在Java ,但我知道Java在标准的TreeMap容器中没有类似的东西。
I want to reproduce this behaviour in Java, but I know that Java doesn't have something like that in standard TreeMap container.
有什么方法保存一个快速路径到TreeMap中的某个元素,这将跳过在这个TreeMap中的对数(时间)搜索?
Is there any way to save a "fast path" to some element in TreeMap, which would skip logarithmic (in time) search in this TreeMap? Should I somehow use references and create my own container, or is there some sort or "magical container" that will fit my needs?
推荐答案
如果您需要有序映射,则可能满足您的需求 - 在splay树中更频繁访问的对象将向顶部倾斜,这会减少其查找时间。有几个Java实现可通过google。另一个选项是维护您从树图访问的最后K个元素的hashmap缓存。
If you need an ordered map, then a splay tree might suit your needs - the more frequently accessed objects in a splay tree will gravitate towards the top, which reduces their lookup time. There are several Java implementations available via google. Another option is to maintain a hashmap cache of the last K elements that you've accessed from the treemap.
这篇关于在Java中对TreeMap进行Concurent修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!