2

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 ?

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Sushant
  • 145
  • 1
  • 2
  • 12
  • 1
    http://stackoverflow.com/questions/10656471/performance-differences-between-arraylist-and-linkedlist – sasikumar Feb 26 '16 at 10:58
  • 1
    http://stackoverflow.com/questions/18734705/which-one-runs-faster-arraylist-or-linkedlist – sasikumar Feb 26 '16 at 10:58
  • Your first sentence is a huge oversimplification. We can't tell without knowing what you're really doing in the code. For most of the use-cases, an ArrayList is the right choice. – JB Nizet Feb 26 '16 at 11:45

3 Answers3

2

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.

Eugene Kirin
  • 568
  • 3
  • 16
  • Hashmaps/hashsets: O(1) for remove/update/insert is on average where m=n(If the number of buckets=m and number of elements = n). Worst case complexity is O(logn) , since Java 8 due to balaned trees under a hash-bucket. – Ivan Voroshilin Feb 26 '16 at 15:26
1

Check these tables at the link below everytime you need to choose a data structure that fits to your problem.
BIG-O Complextity

Nicolò
  • 186
  • 1
  • 16
0

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:

Community
  • 1
  • 1
tucuxi
  • 17,561
  • 2
  • 43
  • 74