Java LinkedList offer() offerFirst() offerLast() Methods Tutorial

In this section, we will learn what the LinkedList offer(), offerFirst(), and offerLast() methods are and how to use them in Java.

What is Java LinkedList offer() Method?

The Java LinkedList offer() method is used to add a method to the end (tail) of a LinkedList object.

Java offer() Method Syntax:

public boolean offer(E e)

offer() Method Parameters:

The method takes one argument and that is the element we want to add to the target LinkedList object.

offer() Method Return Value:

The return value of this method is of type boolean.

If the argument was added to the list, the return value of this method will be true. Otherwise, the value false will return instead.

offer() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList offer() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.offer("John");
        list.offer("Omid");
        list.offer("Jack");
        list.offer("Ellen");
        list.offer("Elon");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Omid

Jack

Ellen

Elon

What is Java LinkedList offerFirst() Method?

The Java LinkedList offerFirst() method is used to add an element to the head of a LinkedList object.

Java offerFirst() Method Syntax:

public boolean offerFirst(E e)

offerFirst() Method Parameters:

The method takes one argument and that is the element we want to add to the head of the target LinkedList object.

offerFirst() Method Return Value:

The return value of this method is of type boolean.

If the argument was added to the list, the return value of this method will be true. Otherwise, the value false will return instead.

offerFirst() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList offerFirst() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.offerFirst("John");
        list.offerFirst("Omid");
        list.offerFirst("Jack");
        list.offerFirst("Ellen");
        list.offerFirst("Elon");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

Elon

Ellen

Jack

Omid

John

What is Java LinkedList offerLast() Method?

The Java LinkedList method is used to add a method as the last element of a LinkedList object.

Note: this method works the same as the offer() method.

Java offerLast() Method Syntax:

public boolean offerLast(E e)

offerLast() Method Parameters:

The method takes one argument and that is the element we want to be added as the last argument of the list.

offerLast() Method Return Value:

The return value of this method is of type boolean.

If the argument was added to the list, the return value of this method will be true. Otherwise, the value false will return instead.

offerLast() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList offerLast() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.offerLast("John");
        list.offerLast("Omid");
        list.offerLast("Jack");
        list.offerLast("Ellen");
        list.offerLast("Elon");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Omid

Jack

Ellen

Elon
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies