Java TreeMap putLast() Method

The TreeMap.putLast() method, introduced in Java 21, allows you to insert a key-value pair at the end of a TreeMap.

Table of Contents

  1. Introduction
  2. putLast Method Syntax
  3. Examples
    • Adding Entries to the End of a TreeMap
    • Handling Duplicate Keys
  4. Real-World Use Case
    • Example: Appending User Activities
  5. Conclusion

Introduction

The TreeMap.putLast() method, introduced in Java 21, allows you to insert a key-value pair at the end of a TreeMap. This method ensures that the key-value pair is placed after all existing entries, regardless of the natural ordering of the keys or any comparator that may be associated with the TreeMap.

putLast() Method Syntax

The syntax for the putLast method is as follows:

public void putLast(K key, V value)
  • The method takes two parameters:
    • key of type K, which represents the key to be inserted.
    • value of type V, which represents the value to be associated with the key.
  • The method does not return a value.

Examples

Adding Entries to the End of a TreeMap

The putLast method can be used to add key-value pairs to the end of a TreeMap.

Example

import java.util.TreeMap;

public class PutLastExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);

        // Adding an entry to the end using putLast
        treeMap.putLast("Vijay", 35);

        // Printing the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}

Output:

TreeMap: {Priya=30, Ravi=25, Vijay=35}

Handling Duplicate Keys

The putLast method will update the value if the key is already present in the TreeMap.

Example

import java.util.TreeMap;

public class DuplicateExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);

        // Adding an entry to the end with a duplicate key
        treeMap.putLast("Ravi", 40); // Updating the value for the key "Ravi"

        // Printing the TreeMap
        System.out.println("TreeMap: " + treeMap);
    }
}

Output:

TreeMap: {Priya=30, Ravi=40}

Real-World Use Case

Example: Appending User Activities

A common real-world use case for TreeMap.putLast() is appending user activities in the order they occur.

Example

import java.util.LinkedHashMap;
import java.util.Map;

public class UserActivityTracker {
    public static void main(String[] args) {
        // Creating a TreeMap to track user activities
        TreeMap<String, String> userActivities = new TreeMap<>();

        // Recording activities
        userActivities.put("Login", "10:00 AM");
        userActivities.put("ViewProfile", "10:05 AM");

        // Adding an activity to the end using putLast
        userActivities.putLast("Logout", "10:15 AM");

        // Printing the user activities
        System.out.println("User Activities: ");
        for (Map.Entry<String, String> entry : userActivities.entrySet()) {
            System.out.println(entry.getKey() + " at " + entry.getValue());
        }
    }
}

Output:

User Activities:
Login at 10:00 AM
ViewProfile at 10:05 AM
Logout at 10:15 AM

In this example, TreeMap.putLast() is used to ensure that user activities are recorded in the order they occur, with new activities appended to the end.

Conclusion

The TreeMap.putLast() method introduced in Java 21 provides a way to add key-value pairs to the end of a TreeMap. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications while ensuring that specific entries are positioned at the end. This method enhances the versatility of TreeMap by allowing specific entries to be appended, making it a useful tool for data management in various scenarios.

Leave a Comment

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

Scroll to Top