在这种情况下,使用什么正确的 Java 集合?

发布于 2024-10-14 23:43:46 字数 104 浏览 3 评论 0原文

我需要在列表或集合中保存大量元素(500k 左右),我需要进行高性能遍历、添加和删除。这将在多线程环境中完成,我不在乎是否能看到遍历开始后完成的更新(弱一致),什么 Java 集合适合这种情况?

I need to hold a large number of elements (500k or so) in a list or a set I need to do high performance traversal, addition and removal. This will be done in a multithreaded environment and I don't care if I gets to see the updates done after traversal began (weakly consistent), what Java collection is right for this scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

表情可笑 2024-10-21 23:43:46

我需要持有大量
列表或 a 中的元素(500k 左右)
设置我需要做高性能
遍历、添加和删除。
...
这将在多线程中完成
环境


ConcrrentSkipListMap - 它不是列表,但列表语义在并发环境中实际上毫无用处。
它将按树状结构对元素进行排序,并且无法通过散列访问,因此您需要一些自然排序(或通过比较器进行外部排序)

如果您只需要在队列末尾添加/删除 - ConcurrentLinkedQueue

如果您期望出现中等程度的争用,则同步集合不适合多线程环境。它们在整个遍历操作期间也需要完全锁定。我也不建议使用 ConcurrentHashMap。

最后:如果您想要真正的多 CPU(如 64+)并期望高争用并且不希望自然排序,请点击链接:http://sourceforge.net/projects/high-scale-lib

I need to hold a large number of
elements (500k or so) in a list or a
set I need to do high performance
traversal, addition and removal.
...
This will be done in a multithreaded
environment


ConcrrentSkipListMap - it's not a List but List semantics are practically useless in concurrent environment.
It will have the elements sorted in a tree alike structure and not accessible via hashing, so you need some natural ordering (or external via comparator)

If you need only add/remove at the ends of a Queue - ConcurrentLinkedQueue.

Synchronized collections are not suited for multi-threaded environment if you expect even moderate contention. They require full lock holding during the entire traverse operation as well. I'd advise against ConcurrentHashMap, either.

In the end: if you are going for real multi-CPU like 64+ and expect high contention and don't want natural ordering follow the link: http://sourceforge.net/projects/high-scale-lib

烟火散人牵绊 2024-10-21 23:43:46

如果遍历 == 读取,并且添加/删除 == 更新,我会说单个集合针对这两种操作进行优化的情况并不常见。

但最好的选择可能是 HashMap

If traversal == read, and add/remove == update, I'd say that it's not often that a single collection is optimized for both operations.

But your best bet is likely to be a HashMap.

(り薆情海 2024-10-21 23:43:46

多线程——所以看看juconcurrent。也许 ConcurrentHashMap 用作 Set - 例如使用 put(x, x) 而不是 add(x)。

Multithreaded - so look at j.u.concurrent. Maybe ConcurrentHashMap used as a Set - e.g. use put(x, x) instead of add(x).

油焖大侠 2024-10-21 23:43:46

如果您经常进行添加和删除,那么“链接”的东西可能是最好的选择。这样,每次添加/删除时,只需更新一个索引,而 ArrayList 则需要“移动”整个数组。问题是你要求的是收藏的圣杯。

查看并发集合可能会有所帮助。

但是“遍历”是什么意思?

If you do addition and removal often, then something "linked" is probably the best choice. That way everytime you add/remove only an index has to be updated, in contrast to an ArrayList for example where the whole Array has to be "moved". The problem is that you are asking for the holy grail of Collections.

Taking a look at the Concurrent Collections might help.

But what do you mean by "traversal"?

动次打次papapa 2024-10-21 23:43:46

如果您需要快速添加或删除列表中间的项目,LinkedList 是一个不错的选择。要在多线程环境中使用它,您需要像这样同步它:

List l = Collections.synchronisedList(new LinkedList());

If you need to add or remove items in the middle of a list quickly, LinkedList is a good choice. To use it in multithreaded enviroment, you need to synchronise it like this:

List l = Collections.synchronisedList(new LinkedList());
落花随流水 2024-10-21 23:43:46

另一方面,由于数据量很大,是否可以将数据存储在数据库中?并使用内存集合作为缓存。

On other hand, due to large size of data, is it possible to store the data in database? And use memory collection as cache.

℡Ms空城旧梦 2024-10-21 23:43:46

是否允许重复项目?

是的,Set不能用。否则你可以使用 SortedSet 。

are duplicate items allowed?

is yes, Set can't be used. you can use SortedSet otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文