使用 List 的哪个实现?
在我的程序中,我经常使用集合来存储对象列表。目前我使用ArrayList来存储对象。 我的问题是:这是最好的选择吗?使用 LinkedList 可能会更好吗?还是别的什么?
要考虑的标准是:
- 内存使用
- 情况 性能
我需要的操作是:
- 将元素添加到集合中
- 迭代元素
有什么想法吗?
更新:我的选择是:ArrayList:)基于此讨论以及以下讨论:
In my program I often use collections to store lists of objects. Currently I use ArrayList to store objects.
My question is: is this a best choice? May be its better to use LinkedList? Or something else?
Criteria to consider are:
- Memory usage
- Performance
Operations which I need are:
- Add element to collection
- Iterate through the elements
Any thoughts?
Update: my choice is : ArrayList :) Basing on this discussion as well as the following ones:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我总是默认使用 ArrayList,在你的情况下也是如此,除非
至于在第二种情况下选择什么,这个 SO.com 线程有一些有用的见解: 列表实现:与 ArrayList 和 TreeList 相比,LinkedList 的性能真的那么差吗?
I always default to ArrayList, and would in your case as well, except when
As to what to pick in that second case, this SO.com thread has some useful insights: List implementations: does LinkedList really perform so poorly vs. ArrayList and TreeList?
我知道我迟到了,但是也许此页面可以帮助您,不仅是现在,还有未来……
I know I'm late but, maybe, this page can help you, not only now, but in the future...
链表对于添加/删除内部元素(即不是头或尾)更快,
Arraylist 的迭代速度更快
Linked list is faster for adding/removing inside elements (ie not head or tail)
Arraylist is faster for iterating
这是插入与检索优化之间的经典权衡。正如您所描述的,该任务的常见选择是 ArrayList。
It's a classic tradeoff between insert vs. retrieve optimization. Common choice for the task as you describe it is the ArrayList.
ArrayList
非常适合您(以及大多数其他)目的。它的内存开销非常小,并且对于大多数操作都具有良好的摊销性能。不理想的情况相对较少:ArrayList
is fine for your (and most other) purposes. It has a very small memory overhead and has good amortized performance for most operations. The cases where it is not ideal are relatively rare:如果您只在列表末尾添加,
ArrayList
应该没问题。来自ArrayList
的文档:,ArrayList 也应该使用比链表更少的内存,因为您不需要为元素使用空间。链接。
If you're only adding at the end of the list,
ArrayList
should be ok. From the documentation ofArrayList
:and
ArrayList
should also use less memory than a linked list as you don't need to use space for the links.这取决于您的使用情况。
您添加到列表的末尾吗?两者都适合这个。
您添加到列表的开头吗? LinkedList 更适合这一点。
您是否需要随机访问(您是否会调用
get(n)
)? ArrayList 更适合这一点。两者都擅长迭代,对于
next()
来说,两个 Iterator 实现的复杂度都是 O(1)。如果有疑问,请使用每个实现测试您自己的应用程序并做出您自己的选择。
It depends on your usage profile.
Do you add to the end of the list? Both are fine for this.
Do you add to the start of the list? LinkedList is better for this.
Do you require random access (will you ever call
get(n)
on it)? ArrayList is better for this.Both are good at iterating, both Iterator implementations are O(1) for
next()
.If in doubt, test your own app with each implementation and make your own choice.
根据您的标准,您应该使用 LinkedList。
LinkedList 实现了 Deque 接口,这意味着它可以在常数时间内 (1) 添加到列表的开头或结尾。另外,ArrayList和LinkedList都会在(N)次内迭代。
您不应该仅仅因为列表已满时添加元素的成本而使用 ArrayList。在这种情况下,添加元素将为 (N),因为正在创建新数组并将所有元素从一个数组复制到另一个数组。
此外,ArrayList 将占用更多内存,因为后备数组的大小可能未完全填满。
Given your criteria, you should be using the LinkedList.
LinkedList implements the Deque interface which means that it can add to the start or end of the list in constant time (1). In addition, both the ArrayList and LinkedList will iterate in (N) time.
You should NOT use the ArrayList simply because the cost of adding an element when the list is full. In this case, adding the element would be (N) because of the new array being created and copying all elements from one array to the other.
Also, the ArrayList will take up more memory because the size of your backing array might not be completely filled.