MediumConcept

What is the difference between ArrayList and LinkedList in Java?

Technology:
Java
Experience:
Junior,
Mid-level

Quick answer

ArrayList is backed by a resizable array with O(1) index access and O(n) insertion in the middle, while LinkedList is a doubly linked list with O(n) index access and O(1) insertion once you hold the node; in practice ArrayList is faster for most workloads.

Detailed explanation

ArrayList stores elements contiguously, which is cache-friendly, so iteration and get(i) are very fast. Adding at the end is amortised O(1); inserting or removing in the middle shifts the following elements.

LinkedList stores each element in a node with previous and next pointers, which costs extra memory and scatters nodes around the heap. Finding the element at position i requires walking the list. It only wins when you repeatedly add or remove at the ends or through an iterator. For queue and deque behaviour, ArrayDeque is usually a better choice than LinkedList.

Key points

  • ArrayList: O(1) get, amortised O(1) add at end
  • LinkedList: O(n) get, extra memory per element
  • Default to ArrayList; use ArrayDeque for queues
  • JavaMedium

    How does HashMap work internally in Java?

    A HashMap stores entries in an array of buckets; it computes the key's hash code to choose a bucket, resolves collisions with a linked list that becomes a red-black tree when a bucket grows large, and resizes when the load factor of 0.75 is exceeded.

    Mid-level 路 Senior 路 Data Structures
  • JavaEasy

    What is the difference between JDK, JRE and JVM in Java?

    The JVM runs Java bytecode, the JRE is the JVM plus the standard libraries needed to run Java programs, and the JDK is the JRE plus the development tools such as the compiler (javac).

    Intern 路 Junior 路 Programming Fundamentals
  • JavaEasy

    What is the difference between == and equals() in Java?

    == compares primitives by value and objects by reference (whether they are the exact same object), while equals() compares the logical content of two objects when the class overrides it.

    Intern 路 Junior 路 Programming Fundamentals
  • JavaEasy

    What is the difference between final, finally and finalize in Java?

    final is a keyword that prevents reassigning a variable, overriding a method or extending a class; finally is a block that always runs after try/catch for cleanup; finalize() was a method called by the garbage collector and is now deprecated.

    Intern 路 Junior 路 Programming Fundamentals