使用 List 的哪个实现?

发布于 2024-09-25 17:07:58 字数 637 浏览 2 评论 0原文

在我的程序中,我经常使用集合来存储对象列表。目前我使用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 技术交流群。

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

发布评论

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

评论(8

难理解 2024-10-02 17:07:58

我总是默认使用 ArrayList,在你的情况下也是如此,除非

  • 我需要线程安全(在这种情况下我开始查看 java.util.concurrent 中的 List 实现)
  • 我知道我将要做大量的插入和操作对列表的操作或分析表明我对 ArrayList 的使用是一个问题(非常罕见)

至于在第二种情况下选择什么,这个 SO.com 线程有一些有用的见解: 列表实现:与 ArrayList 和 TreeList 相比,LinkedList 的性能真的那么差吗?

I always default to ArrayList, and would in your case as well, except when

  • I need thread safety (in which case I start looking at List implementations in java.util.concurrent)
  • I know I'm going to be doing lots of insertion and manipulation to the List or profiling reveals my usage of an ArrayList to be a problem (very rare)

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?

娇妻 2024-10-02 17:07:58

我知道我迟到了,但是也许此页面可以帮助您,不仅是现在,还有未来……

I know I'm late but, maybe, this page can help you, not only now, but in the future...

别把无礼当个性 2024-10-02 17:07:58

链表对于添加/删除内部元素(即不是头或尾)更快,

Arraylist 的迭代速度更快

Linked list is faster for adding/removing inside elements (ie not head or tail)

Arraylist is faster for iterating

三五鸿雁 2024-10-02 17:07:58

这是插入与检索优化之间的经典权衡。正如您所描述的,该任务的常见选择是 ArrayList。

It's a classic tradeoff between insert vs. retrieve optimization. Common choice for the task as you describe it is the ArrayList.

扛刀软妹 2024-10-02 17:07:58

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:

  • The list ist very large
  • You frequently need to do one of these operations:
    • Add/remove items during iteration
    • Remove items from the beginning of the list
你げ笑在眉眼 2024-10-02 17:07:58

如果您只在列表末尾添加,ArrayList 应该没问题。来自ArrayList的文档:

除了添加元素具有恒定的摊销时间成本这一事实之外,没有指定增长策略的细节

,ArrayList 也应该使用比链表更少的内存,因为您不需要为元素使用空间。链接。

If you're only adding at the end of the list, ArrayList should be ok. From the documentation of ArrayList:

The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost

and ArrayList should also use less memory than a linked list as you don't need to use space for the links.

夏尔 2024-10-02 17:07:58

这取决于您的使用情况。

您添加到列表的末尾吗?两者都适合这个。
您添加到列表的开头吗? 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.

橘寄 2024-10-02 17:07:58

根据您的标准,您应该使用 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.

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