简体   繁体   English

ArrayList或Linked List-如果要执行插入/删除和查找操作,则使用哪个集合

[英]ArrayList or Linked List - Which collection to use if both insertion/deletion and look up operation is to be performed

As we know, Linked List is for fast insertion deletion Array List is for fast look up 众所周知,链接列表用于快速插入删除,数组列表用于快速查找

I have requirement of 10000 records to be saved in List. 我要求将10000条记录保存在列表中。 Which collection I would use. 我将使用哪个集合。 Both lookup and insertion / deletion operation may be performed on that list. 查找和插入/删除操作都可以在该列表上执行。

Which Collection do I use and the reason why? 我使用哪个收藏集,为什么? Or Would i use my own created collection ? 还是我会使用自己创建的收藏集?

INSERT 插入

LinkedList

Insert first - O(1) 首先插入-O(1)

Insert last - O(1) 最后插入-O(1)

Insert anywhere - O(n) - it's because need to find by index where to insert. 在任何地方插入-O(n)-这是因为需要按索引查找要插入的位置。

ArrayList

Insert first - O(n) 首先插入-O(n)

Insert last - O(1) 最后插入-O(1)

Insert anywhere - O(n) 插入到任何地方-O(n)


So LinkedList and ArrayList have the same O(n) insert anywhere. 因此, LinkedListArrayList在任何位置都具有相同的O(n)插入。

DELETE 删除

LinkedList

Delete first - O(1) 首先删除-O(1)

Delete last - O(1) 最后删除-O(1)

Delete anywhere - O(n) - And again it's because need to find by index where to delete. 删除任何地方-O(n)-再次是因为需要按索引查找要删除的位置。

ArrayList

Delete first - O(n) 首先删除-O(n)

Delete last - O(1) 最后删除-O(1)

Delete anywhere - O(n) 删除任何地方-O(n)

So LinkedList and ArrayList have the same O(n) delete anywhere. 因此, LinkedListArrayList在任何位置都具有相同的O(n)删除。


As you can see insert and delete anywhere for both is the same. 如您所见,在任何地方插入和删除都是相同的。 If you always do insert last operation then ArrayList is suitable to use because if you know the index then lookup is O(1) and O(n) for LinkedList. 如果您总是插入最后一个操作,则可以使用ArrayList,因为如果您知道索引,则对LinkedList的查找为O(1)和O(n)。 I think you need to find the golden middle what is more suitable to use. 我认为您需要找到最适合使用的黄金中间点。

Also if you dont care about dublicate-free you can use HashSet. 另外,如果您不关心无复制,则可以使用HashSet。 It's based on hash table and provides suitable performence (O(1), O(log(n) for many cases) for insert and delete, lookup. 它基于哈希表,并为插入,删除和查找提供合适的性能(在许多情况下为O(1),O(log(n)))。

HashSet jdoc HashSet Jdoc

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements >properly among the buckets. 该类为基本操作(添加,删除,包含和大小)提供恒定的时间性能,假设哈希函数将元素正确地分散在存储桶中。

Check these tables at the link below everytime you need to choose a data structure that fits to your problem. 每当您需要选择适合您问题的数据结构时,请在下面的链接中查看这些表。
BIG-O Complextity 大复杂性

Without looking at your data, and unless your use-case involves a lot of insertions and deletions using a ListIterator (= the way to get O(1) insertions and deletions on LinkedList when not operating at either end), real-world performance will favor ArrayList due to lower overhead and less pointer dereferencing. 在不查看数据的情况下,除非您的用例涉及使用ListIterator的大量插入和删除操作(=两端都不操作时在LinkedList上获得O(1)插入和删除操作的方式),否则实际性能会由于较低的开销和较少的指针解引用,建议使用ArrayList

Factors that will be important in your choice: 在您选择时很重要的因素:

  1. What is in your lists? 您的清单中有什么? If your lists are collections of small objects (say, Integer ), then overhead from LinkedList will be more noticeable. 如果您的列表是小对象的集合(例如Integer ),则LinkedList开销将更加明显。 Each node in a LinkedList needs at least 2 extra pointers, for an estimated overhead of ~8 bytes per node. LinkedList每个节点至少需要2个额外的指针,每个节点的估计开销约为8个字节。 More memory means more cache misses and lower performance. 更多的内存意味着更多的高速缓存未命中和性能降低。
  2. How exactly will you insert and delete? 您将如何精确地插入和删除? If you only insert and delete at the end of the list, then ArrayList is the way to go. 如果仅在列表末尾插入和删除,则使用ArrayListArrayList的方法。 If you insert and delete in random locations, then ArrayList is still probably the way to go (because of faster scans). 如果您在随机位置插入和删除,则ArrayList可能仍然是可行的方法(因为扫描速度更快)。 Only if you need to insert at the start, and/or make consistent use of ListIterator will you actually see a performance benefit from LinkedList . 仅当您需要在开始时插入和/或始终使用ListIterator时,您才真正从LinkedList看到性能优势。
  3. How unordered will your list be, and how often will you need to perform full scans? 您的列表将变得无序,并且需要多长时间执行一次完整扫描? While in theory all memory accesses take the same time, it is quicker to iterate through elements that are next to each other in RAM (due to processor caches) than to jump all over the place. 虽然理论上所有内存访问都花费相同的时间,但遍历RAM中彼此相邻的元素(由于处理器高速缓存)要比遍历所有元素更快。

Finally - have you considered using a Set or Map to speed up search and access? 最后-您是否考虑过使用SetMap来加快搜索和访问速度? O(n) list access sums up quickly, specially if you use it from within loops. O(n)列表访问快速总结,特别是如果您在循环内使用它。 Maps and Sets can provide O(log n) and O(1) access (depending on implementation), for noticeable performance improvements. 映射和集合可以提供O(log n)O(1)访问(取决于实现),以显着提高性能。

Some references: 一些参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM