Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList vs LinkedList efficiency

I have a problem which uses many insertions in the list at the beginning and afterwards search and retrieval operations are extensively used, So which approach is good and efficient?

Approach 1: Use LinkedList as my data structure for the whole program.

Approach 2: Use ArrayList as my data structure for the whole program.

Approach 3: Use LinkedList as my data structure at the beginning for insertion and do Arraylist al = new Arraylist(ll); for retrieval operations.

How much does the changing of data structure cost?? Is it actually worth doing it?

like image 241
Prithvi Raj Avatar asked Jul 01 '26 17:07

Prithvi Raj


1 Answers

Since they both implement the same interface you can find this out for yourself by writing your code so that the constructor can be plugged in and test your code both ways. Benchmarking can be done with jmh.

You can plug in the constructor by using the Supplier interface.

Depending on the nature of your problem you may find that using a Deque is appropriate.

like image 86
Julian Avatar answered Jul 03 '26 06:07

Julian