LinkedList or ArrayList in Java: What to Choose and Why It Matters

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

LinkedList or ArrayList: Battle of the Java Collection Titans

\n\n

If you've ever written Java code that works with lists of data, you've undoubtedly faced the dilemma: LinkedList or ArrayList? This question is a real hit on Stack Overflow, discussed by thousands of developers. And for good reason. Choosing the wrong data structure can turn your fast code into a turtle crawling through a swamp. The right choice can make your application responsive and efficient.

\n\n

Today, we won't just list dry facts. We'll look under the hood of Java, understand how these collections work at the memory and processor level, and give a clear answer: when should you use LinkedList, and when should you use ArrayList? Let's go!

\n\n

Briefly About the Main Thing: What the Gentlemen from the Documentation Say

\n\n

Let's start with the basics. Both classes implement the List interface, but they do it differently.

\n\n
    \n
  • ArrayList is a dynamic array. It stores elements in a contiguous block of memory. When the array is full, it creates a new, larger one and copies all elements into it.
  • \n
  • LinkedList is a doubly linked list. Each element (node) stores references to the previous and next elements. Memory is allocated for each node separately.
  • \n
\n\n

Sounds simple. But the devil, as always, is in the details. Let's look at the code.

\n\n

Under the Hood: How It Works in Practice

\n\n

ArrayList: Fast Access, but Slow Insertion in the Middle

\n\n
import java.util.ArrayList;\nimport java.util.List;\n\npublic class ArrayListDemo {\n    public static void main(String[] args) {\n        List<String> arrayList = new ArrayList<>();\n        \n        // Adding to the end — fast (O(1) amortized)\n        long start = System.nanoTime();\n        for (int i = 0; i < 100000; i++) {\n            arrayList.add("Element " + i);\n        }\n        long end = System.nanoTime();\n        System.out.println("ArrayList add (to end): " + (end - start) / 1e6 + " ms");\n        \n        // Access by index — instant (O(1))\n        start = System.nanoTime();\n        String element = arrayList.get(50000);\n        end = System.nanoTime();\n        System.out.println("ArrayList get: " + (end - start) + " ns");\n        \n        // Insertion in the middle — slow (O(n))\n        start = System.nanoTime();\n        arrayList.add(25000, "INSERTED");\n        end = System.nanoTime();\n        System.out.println("ArrayList insert (middle): " + (end - start) / 1e6 + " ms");\n    }\n}
\n\n

What's happening here? When you insert an element in the middle of an ArrayList, all elements to the right of the insertion point are shifted by one position. This is an O(n) operation. If you have a million elements, that's a million copy operations. Painful.

\n\n

LinkedList: Insertion is King, but Access is an Outcast

\n\n
import java.util.LinkedList;\nimport java.util.List;\n\npublic class LinkedListDemo {\n    public static void main(String[] args) {\n        List<String> linkedList = new LinkedList<>();\n        \n        // Adding to the end — fast (O(1))\n        long start = System.nanoTime();\n        for (int i = 0; i < 100000; i++) {\n            linkedList.add("Element " + i);\n        }\n        long end = System.nanoTime();\n        System.out.println("LinkedList add (to end): " + (end - start) / 1e6 + " ms");\n        \n        // Access by index — slow (O(n))\n        start = System.nanoTime();\n        String element = linkedList.get(50000);\n        end = System.nanoTime();\n        System.out.println("LinkedList get: " + (end - start) / 1e6 + " ms");\n        \n        // Insertion in the middle — fast (O(1) after finding the position)\n        start = System.nanoTime();\n        // NOTE: the add(index, element) method itself still performs a search (O(n))\n        linkedList                

Blogs

Book Recommendations