Java Language – 26 – Lists (ArrayList, LinkedList, etc.)

Collections and Data Structures – Lists (ArrayList, LinkedList, etc.)
Introduction

Lists are essential data structures in Java, providing dynamic arrays that can grow or shrink as needed. The Java Collections Framework offers various list implementations, with ‘ArrayList’ and ‘LinkedList’ being the most commonly used. In this guide, we’ll explore the concept of lists in Java, understand the differences between ‘ArrayList’ and ‘LinkedList,’ and learn how to use them effectively in your applications.

ArrayList: Dynamic Arrays

‘ArrayList’ is a part of the Java Collections Framework and is implemented as a dynamic array. It provides fast random access and is ideal for scenarios where you need to access elements frequently but don’t need frequent insertions or deletions.

Example – Using ArrayList

Here’s an example of creating and using an ‘ArrayList’:


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

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

        // Adding elements to the ArrayList
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

        // Accessing elements
        System.out.println("First color: " + colors.get(0));

        // Iterating through the ArrayList
        for (String color : colors) {
            System.out.println(color);
        }
    }
}

In this example, we create an ‘ArrayList’ of strings, add elements to it, access elements by index, and iterate through the list.

LinkedList: Doubly Linked Lists

‘LinkedList’ is another list implementation in Java, based on doubly linked lists. It is efficient for frequent insertions and deletions but less efficient for random access.

Example – Using LinkedList

Here’s an example of creating and using a ‘LinkedList’:


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

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

        // Adding elements to the LinkedList
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        // Accessing elements
        System.out.println("First name: " + names.get(0));

        // Iterating through the LinkedList
        for (String name : names) {
            System.out.println(name);
        }
    }
}

In this example, we create a ‘LinkedList’ of strings, add elements to it, access elements by index, and iterate through the list.

Choosing Between ArrayList and LinkedList

When deciding between ‘ArrayList’ and ‘LinkedList,’ consider your application’s specific requirements:

  • ‘ArrayList’ is a better choice when:
    • You require fast random access to elements.
    • You don’t frequently insert or remove elements.
    • Memory usage is not a significant concern.
  • ‘LinkedList’ is a better choice when:
    • You need frequent insertions or deletions in the middle of the list.
    • Iterating through the list is the primary operation.
    • Memory usage is a concern, as ‘LinkedList’ uses more memory due to its node-based structure.
Common List Operations

Lists provide several common operations for managing elements:

  • Adding Elements: Use the ‘add’ method to add elements to the end of the list or specify an index to insert elements at a specific position.
  • Accessing Elements: Use the ‘get’ method to retrieve an element by index.
  • Removing Elements: Use the ‘remove’ method to delete an element by index or object reference.
  • Iterating: Use ‘for-each’ loops or iterators to traverse the list.
Conclusion

Lists are fundamental data structures in Java, providing dynamic storage for elements. ‘ArrayList’ and ‘LinkedList’ are two popular implementations, each suited for specific use cases. By understanding their characteristics and when to use them, you can optimize your Java applications for performance and memory usage.