Java ArrayList addFirst() Method

The ArrayList.addFirst() method, introduced in Java 21, is used to add an element to the beginning of an ArrayList. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. Additionally, we will cover a real-world use case to illustrate its application.

Table of Contents

  1. Introduction
  2. addFirst Method Syntax
  3. How It Works
  4. Examples
    • Adding an Element to the Beginning of the List
    • Handling Edge Cases
  5. Real-World Use Case
  6. Conclusion

Introduction

The ArrayList.addFirst() method is part of the ArrayList class in Java 21. It allows you to add an element to the beginning of the list directly, which simplifies the process of prepending elements.

addFirst Method Syntax

The syntax for the addFirst method is as follows:

public void addFirst(E element)
  • element: The element to be added to the beginning of the list.

How It Works

When you use the addFirst(E element) method, the specified element is inserted at the beginning of the ArrayList. Internally, this involves shifting all existing elements one position to the right to make space for the new element at the first position.

Examples

Adding an Element to the Beginning of the List

The addFirst method can be used to add an element to the beginning of the ArrayList.

Example

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

public class AddFirstExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Banana");
        list.add("Orange");

        // Add an element to the beginning of the list
        ((ArrayList<String>) list).addFirst("Apple");

        System.out.println("ArrayList after addFirst: " + list);
    }
}

Output:

ArrayList after addFirst: [Apple, Banana, Orange]

Handling Edge Cases

The addFirst method should be used with care when the ArrayList is empty to ensure it handles the operation correctly.

Example

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

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

        // Add an element to an empty list
        ((ArrayList<String>) list).addFirst("Apple");

        System.out.println("ArrayList after addFirst to empty list: " + list);
    }
}

Output:

ArrayList after addFirst to empty list: [Apple]

Real-World Use Case

Task Priority Management

In a task management system, you might need to prioritize certain tasks by adding them to the beginning of the task list. The addFirst() method can be used to ensure high-priority tasks are always at the top of the list.

Example

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

class Task {
    String name;
    boolean isHighPriority;

    Task(String name, boolean isHighPriority) {
        this.name = name;
        this.isHighPriority = isHighPriority;
    }

    @Override
    public String toString() {
        return name + (isHighPriority ? " (High Priority)" : "");
    }
}

public class TaskManager {
    public static void main(String[] args) {
        List<Task> tasks = new ArrayList<>();
        tasks.add(new Task("Write report", false));
        tasks.add(new Task("Prepare presentation", false));

        // Add a high priority task at the beginning
        ((ArrayList<Task>) tasks).addFirst(new Task("Fix critical bug", true));

        // Display all tasks
        System.out.println("Task List:");
        tasks.forEach(task -> System.out.println(task));
    }
}

Output:

Task List:
Fix critical bug (High Priority)
Write report
Prepare presentation

Conclusion

The ArrayList.addFirst() method in Java 21 provides a convenient way to add elements to the beginning of an ArrayList. By understanding how to use this method, you can efficiently manage the contents of your lists in Java applications. This method is particularly useful in real-world applications such as task priority management, where prepending elements is necessary.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top