Difference between ArrayList and LinkedList

In this article, we are going to learn the important difference between ArrayList and LinkedList. As it is one of the often-asked interview questions for Java developers, this article is going to give clarity on the topic with in-depth knowledge of both.

Firstly, we will look at each in detail and then we will break down the various key difference between ArrayList vs LinkedList.

ArrayList vs LinkedList

ArrayList is a dynamic array that is flexible, unlike static arrays that need to be instantiated with a fixed size giving it the capability to add or remove elements. Upon its creation, it inherits the AbstractList class and implements the list interface in Java. Under the hood, ArrayList in Java shares exactly the same traits as Vectors in C++.

On the other hand, LinkedList extends the AbstractSequentialList class and implements the Deque and List interfaces. It shares the template characteristics of a linked list with it being stored in non-contiguous spaces and with all the elements having objects containing the twin values of data and address. They use a pointer to reach the next node in a linked list.

Syntax – ArrayList vs LinkedList

Having seen the concepts on ArrayList vs LinkedList, let us look at the syntactical difference between ArrayList and LinkedList.

ArrayList

ArrayList example = new ArrayList();
example.add("Prep");
example.add("Bytes");

Here we are creating an ArrayList of a particular data type, i.e string in this case named example and adding two strings namely, Prep and Bytes to the ArrayList.

LinkedList

LinkedList example =new LinkedList();
example.add("Prep");
 example.add("Bytes");

In a similar fashion to how we created an ArrayList, we create a linked list of a particular data type, i.e string in this case named example and add two strings namely, Prep and Bytes to the LinkedList.

Though the syntax of ArrayList and LinkedList might look similar to each other, it’s the inner working that differs between them with the LinkedList being stored in non-contiguous memory and referred through pointers and ArrayList being stored in contiguous memory locations.

Difference Between ArrayLists and LinkedList

Now that we have some clarity on the working concepts and syntactical differences. Let us look at how ArrayList vs LinkedList goes head to head in each aspect.

ArrayList LinkedList
1. Dynamic Array is used to implement ArrayList internally in JAVA. 1. Doubly Linked List is used to implement LinkedList internally in JAVA
2. ArrayList acts only as a list because it implements a list interface. 2. LinkedList acts as a list as well as a queue because it implements both interfaces.
3. Preferred for storing and accessing the data 3. Preferred for manipulation of data.
4. Accessing data at the nth position occurs in constant time 4. Accessing data at the nth position takes linear time by traversing the LinkedList.
5. Manipulation is comparatively slower than LinkedList because of the shifting of elements upon removal of an element. 5. Manipulation is faster than ArrayList with the reason being that addressing takes care of elements pointing at each other.
6. Storage of elements is performed at a contiguous location. 6. Storage of elements is performed at a non-contiguous location.
7. Being dynamic in nature, ArrayList is resizable. 7. LinkedList is implemented by pointing to elements thereby no sizing issues are faced.
8. The default capacity upon initializing is set as 10 with resizing done upon extension of the list. 8. The is no default capacity as insertion is performed by interlinking of the nodes and no contiguous allocation of space.

Points to Remember about ArrayList vs LinkedList

Now that we have covered most of the parts of ArrayList vs LinkedList, we look at some of the points that we need in mind.

  1. LinkedList consumes extra space for the storage of each element (node) as it has to store the data plus the address to the next node and this follows for all the nodes in the linked list.

  2. The reason that ArrayList elements are less storage-consuming, they are more cache-friendly in comparison to LinkedList.

  3. Both ArrayList and LinkedList can contain duplicate elements, insertion order is maintained and are non-synchronous.

Conclusion
In this article on ArrayList vs LinkedList, we started with the concepts of ArrayLists and LinkedList and proceeded with the key difference between ArrayList and LinkedList as well as the syntactical difference between them.

We hope you liked this article on the topic of ArrayList vs LinkedList and expect to see you again at PrepBytes with another insightful piece of information.

Frequently Asked Question – ArrayList vs LinkedList

1. What is the default size of ArrayList?
The default size of ArrayList is 10 elements. It expands its size upon its expansion.

2. What is better for caching in ArrayList vs LinkedList?
ArrayList is a better option for caching as every object takes less memory in comparison to LinkedList which stores data and addresses.

3. How do both ArrayList and LinkedList fare in terms of data manipulation?
LinkedList does not perform shifting upon removal or insertion, unlike ArrayList which does. Hence, LinkedList is better in terms of data manipulation.

Leave a Reply

Your email address will not be published. Required fields are marked *