[英]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. 因此,
LinkedList
和ArrayList
在任何位置都具有相同的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. 因此,
LinkedList
和ArrayList
在任何位置都具有相同的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: 在您选择时很重要的因素:
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. ArrayList
is the way to go. ArrayList
是ArrayList
的方法。 If you insert and delete in random locations, then ArrayList is still probably the way to go (because of faster scans). ListIterator
will you actually see a performance benefit from LinkedList
. ListIterator
时,您才真正从LinkedList
看到性能优势。 Finally - have you considered using a Set
or Map
to speed up search and access? 最后-您是否考虑过使用
Set
或Map
来加快搜索和访问速度? 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.