Skip to main content

Difference between ArrayList and LinkedList

What is the difference between ArrayList and LinkedList

First of all lets see what Interfaces they implement.
  • ArrayList (See on Oracle/sun site)
    • Serializable
    • Cloneable
    • Iterable<E>
    • Collection<E>
    • List<E>
    • RandomAccess
  • LinkedList(See on Oracle/Sun site)
    • Serializable
    • Cloneable
    • Iterable<E>
    • Collection<E>
    • Deque<E>
    • List<E>
    • Queue<E>
By looking at interface being implemented by these two collection, we could see RandomAccess is implemented by ArrayList and Queue<E>,Deque<E> implemented by LinkedList only. So all the differences between these two collections are because of these three Interfaces and their implementation.

  1. RandomAccess : ArrayList can be accessed randomly, means we could access nth element of an ArrayList directly without affecting search/seek performance. e.g. list.get(n). Means get performance of nth element will never depend on how large is the ArrayList.It will remain constant.
    While LinkedList cant be accessed randomly(though it does have get(0) function but internally its done sequentially), means if you want to access nth element, you will have to access n-1 elements too.So the get/seek performance will always depends on how large is LinkedList and which elemnt you are looking for, smaller is faster , bigger is slower
  2. List Manipulation :
    Insertion at the end : Performance for insertion at the end of ArrayList or LinkedList will remain almost same(ArrayList have slight edge), as linked list keep a pointer at the end of List(Queue,DeQueue) and dont need to traverse other items in the list.
    If you are have requirment just to insert at the end of list and then iterate, think about ArrayList.
    Insertion at the Start: Performance for insertion at the Start of ArrayList will be affected by Size of array already allocated, total number of records in the list. At insert if your underlying array is already full then first array Relocation will happen then all elements after n will be pushed
    but in linked list performance wont be affected by such fators,as linked list keep as pointer at the start of list (Dequeu interface).
    So if you have requirment of inserting items at start and then just iterating, think about LinkedList.
    Insertion at random index :
    ArrayList : In arraylist index item can be reached without any performance degradtion but to insert it the underlying array need to be updated, means all element after n index need to be pushed and that will cost performance.Also there may be memory relocation for underlying array. The worst scenerio will be when you are trying to insert at 0th postion and you have already used underlying allocated array space and best when at the end.
    LinkedList : In Linked list to reach at nth element, first we need to traverse n-1 element(cost performance) and then insert it without any loss of performace(as it was in case of ArrayList). So in case of LinkedList best case is when you insert at 0th location and worst when you insert at end-1 position.
I will do a bench mark later and post result here. If you have becnhmarked and have some chart/graph, let me know i will post it here.


    Popular posts from this blog

    How to add custom user attributes in keycloak and access them in spring boot application

    Sometime it may be possible you want to add more parameters to standard registration page of keyloak for your users and aaccess that data in your spring boot application. This artical will show step by steps on how to add such extra attributes. What is Keycloak Keycloak is an open source software product to allow single sign-on with Identity Management and Access Management aimed at modern applications and services, to learn more visit  h ttps:// What is Spring boot Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". To learn more visit To add an extra attribute in keyclkoak server you will need to edit actuall html template and then registaer new attribute in json response so that it will be available on client. Edit HTML template Lets assume we want to add mobile number on default registration page. Go to Keycloak home installation directory edit fil

    How to run Bash commands in parallel and wait before all commands finished

    In this article i will explain how you can run commands in parallel and make sure you wait until all commands and have finished and then move forward. Say you have a script which downloads few files or yu have to compile different maven project(independent of each other) or something else which can run in parallel and you have got a good machine which can run these tasks in parallel so that overall you can run the full task in lowest time. For this you can use back ground task and wait from unix. When you add & to any command that command runs in background What is wait command? wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell. If wait command is executed this time, then it will be applied for the last command. If no process id or job id is given with wai

    How to create Spring boot cloud config server

    What is Spring boot cloud config server? Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content). The server is embeddable in a Spring Boot application, by using the  @EnableConfigServer  annotation. Consequently, the following application is a config server In Simple terms, Spring boot cloud config server takes your application.yml or from your spring boot application and serve it over HTTP, and spring application which need to use it just need to delete the application.yml or and create one bootstrap.yml/properties and defined 2 simple properties  and Create New java project using one of following instructions. Create project with Java, maven and Intellij Edit POM.xml, add following content in pom.xml First specify the packaging jar also add parent of this pom to be NOTE : You can lookup the latest version