Java LinkedHashSet removeLast() Method (Introduced in Java 21)

In Java 21, the LinkedHashSet class introduced the removeLast() method, allowing you to remove the last element of the collection. 

Table of Contents

  1. Introduction
  2. removeLast Method Syntax
  3. Examples
    • Removing the Last Element in LinkedHashSet
    • Handling an Empty LinkedHashSet
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The LinkedHashSet.removeLast() method is a new addition in Java 21 that allows you to remove the last element of the LinkedHashSet. This method is useful when you need to remove and possibly process the last element of the collection while maintaining the order of insertion.

removeLast() Method Syntax

The syntax for the removeLast method is as follows:

public E removeLast()
  • The method does not take any parameters.
  • The method returns the last element of the LinkedHashSet that was removed.

Examples

Removing the Last Element in LinkedHashSet

The removeLast method can be used to remove the last element of a LinkedHashSet.

Example

import java.util.LinkedHashSet;

public class RemoveLastExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");

        // Removing the last element
        String removedAnimal = animals.removeLast();

        // Printing the removed element and the LinkedHashSet
        System.out.println("Removed last animal: " + removedAnimal);
        System.out.println("LinkedHashSet after removeLast: " + animals);
    }
}

Output:

Removed last animal: Elephant
LinkedHashSet after removeLast: [Lion, Tiger]

Handling an Empty LinkedHashSet

When the LinkedHashSet is empty, calling removeLast will result in an exception. Therefore, it's important to check if the set is empty before calling this method.

Example

import java.util.LinkedHashSet;

public class RemoveLastEmptyExample {
    public static void main(String[] args) {
        // Creating an empty LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Checking if the LinkedHashSet is empty before removing the last element
        if (!animals.isEmpty()) {
            String removedAnimal = animals.removeLast();
            System.out.println("Removed last animal: " + removedAnimal);
        } else {
            System.out.println("The LinkedHashSet is empty.");
        }
    }
}

Output:

The LinkedHashSet is empty.

Real-World Use Case

Use Case: Task Management System

In a task management system, tasks are often managed in the order they are added. There are scenarios where the last task needs to be removed and processed. The removeLast method can be used to achieve this functionality.

Example

import java.util.LinkedHashSet;

public class TaskManagementSystem {
    public static void main(String[] args) {
        // Creating a LinkedHashSet to store tasks
        LinkedHashSet<String> tasks = new LinkedHashSet<>();

        // Adding initial tasks
        tasks.add("Complete project report");
        tasks.add("Email client updates");
        tasks.add("Prepare presentation");

        // Removing and processing the last task
        if (!tasks.isEmpty()) {
            String lastTask = tasks.removeLast();
            System.out.println("Processing last task: " + lastTask);
        } else {
            System.out.println("No tasks available.");
        }

        // Printing the remaining tasks
        System.out.println("Remaining tasks: " + tasks);
    }
}

Output:

Processing last task: Prepare presentation
Remaining tasks: [Complete project report, Email client updates]

Conclusion

The LinkedHashSet.removeLast() method introduced in Java 21 provides a way to remove the last element of a LinkedHashSet. By understanding how to use this method, you can efficiently manage and process elements in your collections. This method is useful for accessing and removing the last inserted element in a predictable manner, making it a valuable tool for collection management in your Java applications. The real-world use case of a task management system illustrates the practical application of this method in removing and processing tasks.

Comments