List in Java: ArrayList and LinkedList


Introduction

In Java, the List interface is a part of the java.util package. Two commonly used implementations of the List interface are:

  • ArrayList
  • LinkedList

1. ArrayList

An ArrayList is a resizable array implementation of the List interface. It is best suited for scenarios where frequent access to elements is required.

Example: Using ArrayList

    import java.util.ArrayList;
    
    public class ArrayListExample {
        public static void main(String[] args) {
            // Create an ArrayList
            ArrayList list = new ArrayList<>();
    
            // Add elements to the ArrayList
            list.add("Apple");
            list.add("Banana");
            list.add("Cherry");
    
            // Display the ArrayList
            System.out.println("ArrayList: " + list);
    
            // Access an element by index
            System.out.println("Element at index 1: " + list.get(1));
    
            // Remove an element
            list.remove("Banana");
            System.out.println("After removal: " + list);
    
            // Iterate over the ArrayList
            System.out.println("Iterating through ArrayList:");
            for (String fruit : list) {
                System.out.println(fruit);
            }
        }
    }
        

2. LinkedList

A LinkedList is a doubly-linked list implementation of the List and Deque interfaces. It is best suited for scenarios where frequent insertions and deletions are required.

Example: Using LinkedList

    import java.util.LinkedList;
    
    public class LinkedListExample {
        public static void main(String[] args) {
            // Create a LinkedList
            LinkedList list = new LinkedList<>();
    
            // Add elements to the LinkedList
            list.add("Dog");
            list.add("Cat");
            list.add("Rabbit");
    
            // Display the LinkedList
            System.out.println("LinkedList: " + list);
    
            // Add an element at a specific index
            list.add(1, "Horse");
            System.out.println("After adding at index 1: " + list);
    
            // Remove an element by index
            list.remove(2);
            System.out.println("After removal: " + list);
    
            // Iterate over the LinkedList
            System.out.println("Iterating through LinkedList:");
            for (String animal : list) {
                System.out.println(animal);
            }
        }
    }
        

Differences Between ArrayList and LinkedList

Feature ArrayList LinkedList
Implementation Uses a dynamic array Uses a doubly-linked list
Access Time Fast (O(1) for get) Slow (O(n) for get)
Insertion/Deletion Slow (O(n)) Fast (O(1) for add/remove at ends)
Memory Less overhead More overhead (due to node pointers)

Conclusion

Both ArrayList and LinkedList have their own strengths and weaknesses. Choose the one that best fits your application's requirements:

  • Use ArrayList for faster random access and when memory is a concern.
  • Use LinkedList for frequent insertions and deletions.




Advertisement