Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java List Collection: ArrayList And LinkedList

ArrayList and LinkedList are two commonly used classes in Java for implementing dynamic lists. Both classes are part of the Java Collections Framework and implement the List interface. This tutorial will cover the basics of ArrayList and LinkedList, including their differences, use cases, and example code.

  • ArrayList:

ArrayList is an implementation of the List interface backed by a dynamic array. It provides fast random access, but slower insertions and deletions compared to LinkedList, especially in the middle of the list.

import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo {
    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();

        // Add elements
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Cherry");

        // Access elements
        System.out.println(arrayList.get(1)); // Output: Banana

        // Remove elements
        arrayList.remove(0);

        // Iterate through the ArrayList
        for (String fruit : arrayList) {
            System.out.println(fruit);
        }
    }
}
  • LinkedList:

LinkedList is an implementation of the List interface backed by a doubly-linked list. It provides fast insertions and deletions, but slower random access compared to ArrayList. LinkedList is suitable for cases where you frequently need to add or remove elements at the beginning or end of the list.

import java.util.LinkedList;
import java.util.List;

public class LinkedListDemo {
    public static void main(String[] args) {
        List<String> linkedList = new LinkedList<>();

        // Add elements
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Cherry");

        // Access elements
        System.out.println(linkedList.get(1)); // Output: Banana

        // Remove elements
        linkedList.remove(0);

        // Iterate through the LinkedList
        for (String fruit : linkedList) {
            System.out.println(fruit);
        }
    }
}
  • Differences between ArrayList and LinkedList:
  • Internal structure: ArrayList uses a dynamic array, while LinkedList uses a doubly-linked list.
  • Random access: ArrayList provides faster random access (O(1)) than LinkedList (O(n)).
  • Insertion and deletion: LinkedList provides faster insertion and deletion (O(1)) than ArrayList (O(n) in the worst case).
  • Memory overhead: LinkedList has a higher memory overhead due to storing pointers for each element.
  • Choosing between ArrayList and LinkedList:
  • Use ArrayList when:

    • You need frequent random access to elements.
    • You have infrequent insertions and deletions in the middle of the list.
    • You want to minimize memory overhead.
  • Use LinkedList when:

    • You have frequent insertions and deletions at the beginning or end of the list.
    • Random access is not a primary concern.

This tutorial covered the basics of ArrayList and LinkedList in Java, their differences, use cases, and example code. Choosing between ArrayList and LinkedList depends on the specific requirements of your program and the operations you'll be performing most frequently.

  1. Creating and initializing ArrayList in Java

    // Creating and initializing ArrayList
    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("Apple");
    arrayList.add("Banana");
    arrayList.add("Orange");
    
  2. Adding and removing elements in ArrayList and LinkedList

    // Adding elements
    arrayList.add("Grapes");
    
    // Removing elements
    arrayList.remove("Banana");
    
  3. Iterating through ArrayList and LinkedList in Java

    // Iterating through ArrayList
    for (String fruit : arrayList) {
        System.out.println(fruit);
    }
    
  4. Searching for elements in ArrayList and LinkedList

    // Searching for elements
    if (arrayList.contains("Apple")) {
        System.out.println("Apple found!");
    }
    
  5. Memory usage comparison of ArrayList and LinkedList

    ArrayList uses less memory per element than LinkedList due to its dynamic array implementation.

  6. Sorting ArrayList and LinkedList in Java

    // Sorting ArrayList
    Collections.sort(arrayList);
    
    // Sorting LinkedList
    Collections.sort(linkedList);
    
  7. Converting ArrayList to LinkedList and vice versa

    // Converting ArrayList to LinkedList
    LinkedList<String> linkedList = new LinkedList<>(arrayList);
    
    // Converting LinkedList to ArrayList
    ArrayList<String> arrayListConverted = new ArrayList<>(linkedList);