简体   繁体   中英

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. 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)

Insert last - O(1)

Insert anywhere - O(n) - it's because need to find by index where to insert.

ArrayList

Insert first - O(n)

Insert last - O(1)

Insert anywhere - O(n)


So LinkedList and ArrayList have the same O(n) insert anywhere.

DELETE

LinkedList

Delete first - O(1)

Delete last - O(1)

Delete anywhere - O(n) - And again it's because need to find by index where to delete.

ArrayList

Delete first - O(n)

Delete last - O(1)

Delete anywhere - O(n)

So LinkedList and ArrayList have the same O(n) delete anywhere.


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. 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. It's based on hash table and provides suitable performence (O(1), O(log(n) for many cases) for insert and delete, lookup.

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.

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. Each node in a LinkedList needs at least 2 extra pointers, for an estimated overhead of ~8 bytes per node. 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. If you insert and delete in random locations, then ArrayList is still probably the way to go (because of faster scans). 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 .
  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.

Finally - have you considered using a Set or Map to speed up search and access? O(n) list access sums up quickly, specially if you use it from within loops. Maps and Sets can provide O(log n) and O(1) access (depending on implementation), for noticeable performance improvements.

Some references:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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