Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java 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.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M