What is faster: LinkedList or ArrayList?

In this post I test speed adding and getting elements to and from list. I create MyList project in Netbeans IDE. In main class I write two methods: addElement and getElement.

My first call is for 20 000 elements. It is quite a lot of elements. Adding elements is turn in the loop.

Result:

So ArrayList is slightly faster for 20 000 element in adding elements and much faster in get element than LinkedList. So if You will be often getting elements from list apply ArrayList.

Another call these methods are for 20 000 elements and adding elements to 0 index on the list.

In result You see that for adding elements to 20 000 elements of the list LinkedList is much faster. But in this situation for get elements from list faster is ArrayList.

Another call methods are list of 3500 elements.

For turn adding elements and getting its from list faster is ArrayList.

But if adding elements is only for 0 index…

In this situation for add faster is LinkedList, but only marginally and getting elements from list much faster is ArrayList.

Last calling is for a lot of elements of the list: 53 500.

For adding elements one after the other in the loop, faster is ArrayList and in getting elements much faster is ArrayList.

If for 53 500 elements calling methods with adding to 0 index in the list …

For adding elements much faster is LinkedList and for getting elements from list much faster is ArrayList.

So if you have a small list apply ArrayList, but if You operating on the a lot of elements and getting these elements – apply ArrayList, if adding or remove elements from list – apply LinkedList.