Java中TreeMap的并发修改 [英] Concurrent modification of TreeMap in Java

查看:490
本文介绍了Java中TreeMap的并发修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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?

推荐答案

Java中的 std :: unordered_map 相当于 HashMap ConcurrentHashMap ,它们有预期的恒定时间查找。 TreeMap 是有序地图

The equivalent to a std::unordered_map in Java is a HashMap or ConcurrentHashMap, which have expected constant-time lookups. A TreeMap is an ordered map

如果您需要有序地图,那么 splay tree 可能适合您的需求 - 展开树中访问频率较高的对象会向顶部移动,这会减少查找时间。有几种Java实现可以通过谷歌。另一个选择是维护您从树形图中访问的最后K个元素的哈希映射缓存。

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的并发修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆