In this post, we will discuss the difference between ArrayList and LinkedList in Java.
Learn and master Java Collections Framework at https://www.javaguides.net/p/java-collections-tutorial.htmlThis is one of the frequently asked Java interview question in Java interviews so let's discuss the difference between ArrayList and LinkedList in Java.
Difference between ArrayList and LinkedList in Java
The below diagram summaries the between ArrayList and LinkedList in Java:Both ArrayList and LinkedList implement the List interface. However, they differ completely in the way they store and link to the elements.
Following are some key differences between LinkedList and ArrayList:
- An ArrayList stores the elements sequentially based on their index. However, a LinkedList uses a doubly-linked list to store its elements.
- A LinkedList consumes more memory than an ArrayList because it also stores the next and previous references along with the data.
- You can access an element in an ArrayList is O(1) time. But it takes O(n) time to access an element in a LinkedList because it needs to traverse to the desired element by following the next/prev references.
- Adding or removing elements are usually slower in an ArrayList compared to LinkedList. This is because the elements in the ArrayList needs to be shifted if a new element is added in the middle of the ArrayList. The ArrayList might also need to be resized to accommodate the new element. Similarly, in case of removal, the elements in the ArrayList needs to be shifted to the new positions.
YouTube Video
This difference explained very well in below YouTube video:
Related Java Interview Articles
- Spring Boot Interview Questions
- Java Tricky Coding Interview Questions
- Java String Interview Questions
- Java String Tricky Coding Questions
- Java main() Method Interview Questions
- Java 8 Interview Questions
- Top 10 Spring MVC Interview Questions
- Java OOPS Tricky Coding Questions
- Java Programs Asked in Interview
- OOPS Interview Questions and Answers
- Hibernate Interview Questions
- JPA Interview Questions and Answers
- Java Design Patterns Interview Questions
- Spring Core Interview Questions
- Java Exception Handling Interview
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course