Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList offerLast() method
Syntax and Examples


Java LinkedList offerLast() Method

Java LinkedList offerLast() Method

The offerLast() method in Java's LinkedList class provides a way to add an element to the end of the list. Unlike some other methods, it offers a non-blocking approach – if the list is full (capacity exceeded), it gracefully returns false instead of throwing an exception or waiting indefinitely.

Syntax


public boolean offerLast(E e)

Let's break down what this syntax means:

  • offerLast(): This is the name of the method.
  • (E e): This indicates that the method accepts a single argument, which represents the element you want to add to the end of the list. E is a generic type parameter representing the type of elements stored in the LinkedList.
  • boolean: The return type; it tells us whether the operation was successful or not.

Parameters

Parameter Description
e The element to be added to the end of the list. Its type must match the generic type parameter E declared for the LinkedList.

Return Value

The method returns a boolean value:

  • true: If the element was successfully added to the end of the list.
  • false: If the element could not be added, typically because the list is full (if it has a capacity limit). Note that LinkedLists do not inherently have capacity limits. This behavior would only come from wrapping a LinkedList in another class with such limits.

Examples

Adding a String to a LinkedList

In this example, we'll create a LinkedList of Strings and use offerLast() to add a string element.


import java.util.LinkedList;

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

        boolean success = myList.offerLast("Hello");

        if (success) {
            System.out.println("Element added successfully.");
            System.out.println(myList);
        } else {
            System.out.println("Failed to add element.");
        }
    }


Element added successfully.
[Hello]

Explanation: We create a LinkedList named myList to hold strings. We then call offerLast() with the string “Hello”. The element is successfully added, and we print a success message along with the list's content.

Adding an Integer to a LinkedList

This example demonstrates adding an integer element using offerLast().


import java.util.LinkedList;

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

        boolean success = myList.offerLast(10);
        System.out.println("Success: " + success);
        System.out.println(myList);
    }


Success: true
[10]

Explanation: Similar to the previous example, we create a LinkedList for integers and add the integer 10 using offerLast(). The output confirms that the element was added successfully.

Handling Potential Failure

This demonstrates how to check if offerLast() failed (though this is rare with a standard LinkedList).


import java.util.LinkedList;

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

        boolean success1 = myList.offerLast("First");
        boolean success2 = myList.offerLast("Second");
         //Simulate a full list (not really possible with default LinkedList)

        if (!success1) {
            System.out.println("Failed to add the first element.");
        } else{
             System.out.println("First element added successfully.");
        }
          if (!success2) {
            System.out.println("Failed to add the second element.");
        } else{
             System.out.println("Second element added successfully.");
        }
    }


First element added successfully.
Second element added successfully.

Explanation: We add two strings to the list and check if each operation succeeded. With a regular LinkedList, this will always return true.


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