具有 Queue 基本功能的最快 Java 集合是什么? [英] What is the fastest Java collection with the basic functionality of a Queue?

查看:16
本文介绍了具有 Queue 基本功能的最快 Java 集合是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中最快的集合是什么?

What is the fastest collection in Java?

我只需要添加和删除操作,顺序不重要,等于元素不是问题,重要的是添加和删除.

I only need the operations to add and remove, order is not important, equals elements is not a issue, nothing more than add and remove is imporant.

没有限制大小也很重要.

Without limit size is important too.

这些集合里面会有对象.

These collection will have Objects inside him.

目前我正在使用 ArrayDeque,因为我认为这是更快的队列实现.

Currently I'm using ArrayDeque because I see this is the faster Queue implementation.

推荐答案

ArrayDeque 是最好的.请参阅该基准测试,它来自这篇博文关于对此进行基准测试的结果.ArrayDeque 没有 LinkedList 所具有的节点分配开销,也没有 ArrayList 具有的在 remove 时移动数组内容的开销.在基准测试中,它对于大队列的性能约为 3xLinkedList,对于空队列的性能甚至略优于 ArrayList.为了获得最佳性能,您可能希望为其提供足够大的初始容量,以容纳一次可能容纳的元素数量,以避免多次调整大小.

ArrayDeque is best. See this benchmark, which comes from this blog post about the results of benchmarking this. ArrayDeque doesn't have the overhead of node allocations that LinkedList does nor the overhead of shifting the array contents left on remove that ArrayList has. In the benchmark, it performs about 3x as well as LinkedList for large queues and even slightly better than ArrayList for empty queues. For best performance, you'll probably want to give it an initial capacity large enough to hold the number of elements it's likely to hold at a time to avoid many resizes.

ArrayListLinkedList 之间,似乎取决于队列在任何给定时间将包含的总元素的平均数量以及LinkedListcode> 比 ArrayList 从大约 10 个元素开始.

Between ArrayList and LinkedList, it seems that it depends on the average number of total elements the queue will contain at any given time and that LinkedList beats ArrayList starting at about 10 elements.

这篇关于具有 Queue 基本功能的最快 Java 集合是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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