Java LinkedHashSet addLast(E e) Method (Introduced in Java 21)

In Java 21, the LinkedHashSet class introduced the addLast(E e) method, allowing elements to be added as the last element of the collection.

Table of Contents

  1. Introduction
  2. addLast Method Syntax
  3. Examples
    • Adding an Element as the Last Element in LinkedHashSet
    • Maintaining Insertion Order with addLast
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The LinkedHashSet.addLast(E e) method is a new addition in Java 21 that allows you to add an element as the last element of the LinkedHashSet. This method helps maintain the desired insertion order by explicitly adding elements to the end of the set.

addLast Method Syntax

The syntax for the addLast method is as follows:

public void addLast(E e)
  • The method takes a single parameter e of type E, which represents the element to be added to the LinkedHashSet.
  • The method does not return any value.

Examples

Adding an Element as the Last Element in LinkedHashSet

The addLast method can be used to add an element at the end of a LinkedHashSet.

Example

import java.util.LinkedHashSet;

public class AddLastExample {
    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");

        // Adding an element as the last element
        animals.addLast("Giraffe");

        // Printing the LinkedHashSet
        System.out.println("LinkedHashSet after addLast: " + animals);
    }
}

Output:

LinkedHashSet after addLast: [Lion, Tiger, Elephant, Giraffe]

Maintaining Insertion Order with addLast

When you use addLast, the new element is added to the end of the set, maintaining the insertion order.

Example

import java.util.LinkedHashSet;

public class AddLastOrderExample {
    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");

        // Adding an element as the last element
        animals.addLast("Giraffe");

        // Adding another element as the last element
        animals.addLast("Zebra");

        // Printing the LinkedHashSet
        System.out.println("LinkedHashSet after multiple addLast: " + animals);
    }
}

Output:

LinkedHashSet after multiple addLast: [Lion, Tiger, Giraffe, Zebra]

Real-World Use Case

Use Case: Task Management System

In a task management system, tasks are typically added in the order they are created. However, there are scenarios where a task might need to be added specifically to the end of the list. The addLast 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");

        // Adding a low-priority task at the end
        tasks.addLast("Review team feedback");

        // Printing the tasks in order of priority
        System.out.println("Tasks in order of priority: " + tasks);
    }
}

Output:

Tasks in order of priority: [Complete project report, Email client updates, Prepare presentation, Review team feedback]

Conclusion

The LinkedHashSet.addLast(E e) method introduced in Java 21 provides a way to add elements at the end of a LinkedHashSet. By understanding how to use this method, you can efficiently manage the order of elements in your collections. This method ensures that you can explicitly control the insertion order, 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 managing tasks with varying priorities.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top