BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Difference between ArrayList and LinkedList in Java

Last Updated: July 1, 2024 by Chaitanya Singh | Filed Under: java

In this guide, you will learn difference between ArrayList and LinkedList in Java. ArrayList and LinkedList both implements List interface and their methods and results are almost identical. However there are few differences between them which make one better over another on case to case basis.

ArrayList Vs LinkedList

ArrayListLinkedList
ArrayList class inherits the features of list as it implements the List interface.LinkedList class has the features of list and queue both as it implements both List and Dequeue interfaces.
ArrayList data structure is similar to array as the ArrayList elements are stored in contiguous locations.LinkedList elements are not stored in contagious locations. This is because LinkedList consists of nodes where each node has data field and reference to the next node in the list.
ArrayList default capacity is 10. If more than 10 elements are added to the ArrayList, its capacity gets doubled to accommodate new elements.LinkedList default capacity is zero. When LinkedList is created its an empty list without any initial capacity.
ArrayList uses dynamic array to store the elements.LinkedList uses concept of doubly linked list to store the elements.
ArrayList gives better performance for add and search operations.LinkedList gives better performance for data deletion.
Memory consumption is low in ArrayList as it stores only the elements data in contiguous locations.Memory consumption is high in LinkedList as it maintains element data and two pointers for neighbour nodes, hence the memory consumption is high in LinkedList.

Performance difference between ArrayList and LinkedList for various operations

1) Search: ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1) while LinkedList performance is O(n).

Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

2) Deletion: LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).

Conclusion: LinkedList element deletion is faster compared to ArrayList.

Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbour elements in the list. Hence removal only requires change in the pointer location in the two neighbour nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element.

3) Inserts Performance: LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. This is because every time you add an element, Java ensures that it can fit the element so it grows the ArrayList. If the ArrayList grows faster, there will be a lot of array copying taking place. In worst-case the array must be resized and copied.

There are few similarities between these classes which are as follows:

  1. Both ArrayList and LinkedList are implementation of List interface.
  2. They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.
  3. Both of these classes are non-synchronized and can be made synchronized explicitly by using Collections.synchronizedList method.
  4. The iterator and listIterator returned by these classes are fail-fast (if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException).

When to use LinkedList and when to use ArrayList?

1) As explained above the insert and remove operations give good performance (O(1)) in LinkedList compared to ArrayList(O(n)). Hence if there are frequent addition and deletion in application then LinkedList is a best choice.

2) Search (get method) operations are fast in Arraylist (O(1)) but not in LinkedList (O(n)) so If there are less add and remove operations and more search operations requirement, ArrayList would be your best bet.

Example of ArrayList and LinkedList in Java

In this example, we are demonstrating the use of ArrayList and LinkedList in Java. Here we have initialized an arraylist arrList and a linkedlist linkList. We have added few elements to both arrList and linkList. In the end, elements of both the lists are printed.

import java.util.*;
class JavaExample{
  public static void main(String args[]){

    //ArrayList
    ArrayList<String> arrList=new ArrayList<>();
    arrList.add("Apple");
    arrList.add("Orange");
    arrList.add("Banana");
    arrList.add("Mango");

    //LinkedList
    LinkedList<String> linkList=new LinkedList<>();
    linkList.add("Beans");
    linkList.add("Tomato");
    linkList.add("Lemon");
    linkList.add("Potato");

    //printing elements
    System.out.println("ArrayList elements: "+ arrList);
    System.out.println("LinkedList elements: "+ linkList);
  }
}

Output:

Difference between ArrayList and LinkedList in Java

Recommended guides:

  • ArrayList vs Vector
  • ArrayList vs HashMap
❮ Java ArrayListJava Collections ❯

Top Related Articles:

  1. StringJoiner toString() Method in Java
  2. How to loop HashMap in java
  3. Add Multiple Items to an ArrayList in Java
  4. Java StringBuffer toString()
  5. Java StringBuilder append() Method

Tags: Collections, Java-ArrayList, Java-LinkedList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. krishna kumar gupta says

    June 19, 2014 at 7:38 AM

    really great explanations

    Reply
  2. Subhankar Adhikary says

    July 7, 2014 at 7:09 PM

    I am new in java. I want to know basic difference between ArrayList and LinkList. This is nice article. Nice Job. Nice explained. Its really helps me.

    Reply
  3. vairam says

    September 12, 2014 at 3:06 PM

    very gud explanation. Superb.

    Reply
  4. enadun says

    September 28, 2014 at 4:17 PM

    Thanks a lot. Fully satisfied answer !

    Reply
  5. Anshuman Dwivedi says

    November 12, 2014 at 3:51 AM

    You said –
    The iterator and listIterator returned by these classes are fail-fast (if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods.
    How are you going to add element via iterator ?

    Reply
  6. Subbareddy says

    December 22, 2014 at 1:46 PM

    Hi,
    This post is really good. But, In the above I have one doubt. while searching arraylist follows O(1) because of index. At the same time why arraylist will not follow same one while deletion.
    regards,
    Subbareddy

    Reply
    • Neel Mehta says

      January 20, 2015 at 6:12 AM

      ArrayList doesn’t follow the same for delete since even though it can find the element at the given index fairly quickly, it has to shift all the elements at the later indices back one so that the empty index of the deleted element gets filled.

      Reply
      • Mfily says

        June 25, 2015 at 9:52 PM

        It’s really a nice post but i want to know the definition of array list and linked list I can’t get the definition.

        Reply
    • Prince Abhijeet says

      February 20, 2017 at 5:00 PM

      You see: ArrayList has indexes and LinkedList has pointer to next node. So deleting an item of ArrayList will cause next items to shift left so shifting takes more time. Whereas in case of LinkedList when an item is deleted there is no need to shift left the next items, only thing is needed is to point the pointer to the next node.

      Reply
    • Anonymous007 says

      June 20, 2017 at 5:33 AM

      An arrayList stores data at contiguous memory locations, so when deletion operation is performed it creates an empty space in arrayList. This empty space has to be occupied again by performing large number of shift operations. And hence DELETION from arrayList does not give O(1).

      Reply
  7. robert says

    August 8, 2015 at 10:17 AM

    why isn’t array list implemented to insert an item to the end -> so no reindexing would be needed and the insert operation would be O(1) ? If I wanted to delete an item from linked list, wouldn’t it be needed to search for the item firstly (so it would be O(n) ) ?

    Reply
  8. Michael says

    November 24, 2015 at 8:16 AM

    Dear Robert, this is what I asked myself, too. I looked into the code and remove has O(n). If you use iterator for remove or add, then you have O(1).

    See Stackoverflow: http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist

    Reply
  9. Paras Mehta says

    December 2, 2015 at 1:19 PM

    Isn’t array list add is O(1) as it adds element at the end. Insertion order is maintained. So add should be O(1) only….

    Reply
  10. Wasim says

    July 5, 2016 at 1:03 PM

    Superb explanation :)

    Reply
  11. Ilia says

    July 28, 2016 at 11:24 AM

    Great, thanks! And what about editing the data? Which one is better if you change it a lot? Like if the type is integer and you constantly increase the values of the items. Sorry, if the question is stupid.

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap