在这种情况下,使用什么正确的 Java 集合?
我需要在列表或集合中保存大量元素(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
ConcrrentSkipListMap - 它不是列表,但列表语义在并发环境中实际上毫无用处。
它将按树状结构对元素进行排序,并且无法通过散列访问,因此您需要一些自然排序(或通过比较器进行外部排序)
如果您只需要在队列末尾添加/删除 - ConcurrentLinkedQueue。
如果您期望出现中等程度的争用,则同步集合不适合多线程环境。它们在整个遍历操作期间也需要完全锁定。我也不建议使用 ConcurrentHashMap。
最后:如果您想要真正的多 CPU(如 64+)并期望高争用并且不希望自然排序,请点击链接:http://sourceforge.net/projects/high-scale-lib
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
这是一篇关于根据您的应用程序选择集合的非常好的文章
http://www.developer.com/java/article.php/3829891/Selecting-the-Best-Java-Collection-Class-for-Your-Application.htm
你也可以尝试一下
http://www.javamex.com/tutorials /collections/how_to_choose.shtml
Here is a very good article on selecting a collection depending on your application
http://www.developer.com/java/article.php/3829891/Selecting-the-Best-Java-Collection-Class-for-Your-Application.htm
you can try this as well
http://www.javamex.com/tutorials/collections/how_to_choose.shtml
如果遍历 == 读取,并且添加/删除 == 更新,我会说单个集合针对这两种操作进行优化的情况并不常见。
但最好的选择可能是 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.
多线程——所以看看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).
如果您经常进行添加和删除,那么“链接”的东西可能是最好的选择。这样,每次添加/删除时,只需更新一个索引,而 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"?
如果您需要快速添加或删除列表中间的项目,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:
另一方面,由于数据量很大,是否可以将数据存储在数据库中?并使用内存集合作为缓存。
On other hand, due to large size of data, is it possible to store the data in database? And use memory collection as cache.
是否允许重复项目?
是的,Set不能用。否则你可以使用 SortedSet 。
are duplicate items allowed?
is yes, Set can't be used. you can use SortedSet otherwise.