Java Reference Library →

LinkedList addFirst() method - Syntax and Examples

LinkedList addFirst() method - Syntax and Examples

Learn about the Java LinkedList's addFirst() method. This tutorial explains its syntax, parameters, return value, and provides practical examples.

This reference explains LinkedList addFirst() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

LinkedList addFirst() method - Syntax and Examples

Introduction

The addFirst() method in Java's LinkedList class is a handy tool for inserting elements at the beginning of your list. Think of it like adding someone to the front of a line – they become the very first person!

Syntax

public void addFirst(E e)

Parameters

Parameter Description
e The element you want to add to the beginning of the LinkedList. The type is E, which represents the generic type of elements stored in the list (e.g., Integer, String).

Return Value

This method doesn't return anything directly; it returns void. It modifies the LinkedList by adding the element to the front.

Examples

Adding a String to the Beginning of a List

Let's start with a simple example: adding a string to the beginning of a LinkedList.

import java.util.LinkedList;

public class AddFirstExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>();

        // Add some initial elements
        myList.add("Second Element");
        myList.add("Third Element");

        System.out.println("List before addFirst: " + myList);

        // Add 'First Element' to the beginning
        myList.addFirst("First Element");

        System.out.println("List after addFirst: " + myList);
    }
}
List before addFirst: [Second Element, Third Element]
List after addFirst: [First Element, Second Element, Third Element]

In this example, we created a LinkedList of Strings. We added two elements initially and then used addFirst() to insert “First Element” at the beginning. Notice how the order changed!

Adding an Integer to the Beginning

Now let's see how it works with integers.

import java.util.LinkedList;

public class AddFirstIntegerExample {
    public static void main(String[] args) {
        LinkedList<Integer> myList = new LinkedList<>();

        //Add some initial elements
        myList.add(2);
        myList.add(3);
        System.out.println("List before addFirst: " + myList);

        // Add 1 to the beginning
        myList.addFirst(1);

        System.out.println("List after addFirst: " + myList);
    }
}
List before addFirst: [2, 3]
List after addFirst: [1, 2, 3]

This example is similar to the previous one, but this time we're working with a LinkedList of Integers. We added 2 and 3, then used addFirst() to place 1 at the front.

Adding Different Data Types

LinkedLists are flexible; you can add different data types as long as your list is declared for that type or uses Object. Here’s an example using a mixed list (though generally, it's best to stick with one type per LinkedList).

import java.util.LinkedList;

public class AddFirstMixedExample {
    public static void main(String[] args) {
        LinkedList<Object> myList = new LinkedList<>();

        myList.add("Hello");
        myList.add(123);
        System.out.println("List before addFirst: " + myList);
        // Add a Double to the beginning
        myList.addFirst(3.14);
        System.out.println("List after addFirst: " + myList);
    }
}
List before addFirst: [Hello, 123]
List after addFirst: [3.14, Hello, 123]

Here we declared a list of type Object and added both a String and an Integer. Then we used addFirst() to put a Double (3.14) at the beginning.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →