Remove the First Element from a List

1. Overview

In this super-quick tutorial, we’ll show how to remove the first element from a List.

We’ll perform this operation for two common implementations of the List interface – ArrayList and LinkedList.

2. Creating a List

Firstly, let’s populate our Lists:

@Before
public void init() {
    list.add("cat");
    list.add("dog");
    list.add("pig");
    list.add("cow");
    list.add("goat");

    linkedList.add("cat");
    linkedList.add("dog");
    linkedList.add("pig");
    linkedList.add("cow");
    linkedList.add("goat");
}

3. ArrayList

Secondly, let’s remove the first element from the ArrayList, and make sure that our list doesn’t contain it any longer:

@Test
public void givenList_whenRemoveFirst_thenRemoved() {
    list.remove(0);

    assertThat(list, hasSize(4));
    assertThat(list, not(contains("cat")));
}

As shown above, we’re using remove(index) method to remove the first element – this will also work for any implementation of the List interface.

4. LinkedList

LinkedList also implements remove(index) method (in its own way) but it has also the removeFirst() method.

Let’s make sure that it works as expected:

@Test
public void givenLinkedList_whenRemoveFirst_thenRemoved() {
    linkedList.removeFirst();

    assertThat(linkedList, hasSize(4));
    assertThat(linkedList, not(contains("cat")));
}

5. Time Complexity

Although the methods look similar, their efficiency differs. ArrayList‘s remove() method requires O(n) time, whereas LinkedList‘s removeFirst() method requires O(1) time.

This is because ArrayList uses an array under the hood, and the remove() operation requires copying the rest of the array to the beginning. The larger the array is, the more elements need to be shifted.

Unlike that, LinkedList uses pointers meaning that each element points to the next and the previous one.

Hence, removing the first element means just changing the pointer to the first element. This operation always requires the same time not depending on the size of a list.

6. Conclusion

In this article, we’ve covered how to remove the first element from a List, and have compared the efficiency of this operation for ArrayList and LinkedList implementations.

As usual, the complete source code is available over on GitHub.

Related posts:

Java Program to Implement Binary Heap
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Getting the Size of an Iterable in Java
How to Get All Dates Between Two Dates?
Using the Map.Entry Java Class
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement PriorityBlockingQueue API
Spring MVC Custom Validation
An Introduction to ThreadLocal in Java
Functional Interfaces in Java 8
The Thread.join() Method in Java
The Spring @Controller and @RestController Annotations
An Intro to Spring Cloud Vault
Setting the Java Version in Maven
Spring Data – CrudRepository save() Method
Jackson – JsonMappingException (No serializer found for class)
Java – Write to File
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
ETags for REST with Spring
Apache Commons Collections SetUtils
Send email with SMTPS (eg. Google GMail)
Introduction to Spring Method Security
Java Collections Interview Questions
HttpClient Connection Management
Encode a String to UTF-8 in Java
Spring Boot - Rest Controller Unit Test
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph