Java TreeMap putFirst() Method

The TreeMap.putFirst() method, introduced in Java 21, allows you to insert a key-value pair at the beginning of a TreeMap. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.putFirst() can be used effectively.

Table of Contents

  1. Introduction
  2. putFirst Method Syntax
  3. Examples
    • Adding Entries to the Beginning of a TreeMap
    • Handling Duplicate Keys
  4. Real-World Use Case
    • Example: Prioritizing Urgent Tasks
  5. Conclusion

Introduction

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

putFirst() Method Syntax

The syntax for the putFirst method is as follows:

public void putFirst(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 Beginning of a TreeMap

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

Example

import java.util.TreeMap;

public class PutFirstExample {
    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 beginning using putFirst
        treeMap.putFirst("Vijay", 35);

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

Output:

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

Handling Duplicate Keys

The putFirst 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 beginning with a duplicate key
        treeMap.putFirst("Ravi", 40); // Updating the value for the key "Ravi"

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

Output:

TreeMap: {Ravi=40, Priya=30}

Real-World Use Case

Example: Prioritizing Urgent Tasks

A common real-world use case for TreeMap.putFirst() is prioritizing urgent tasks in a task management application.

Example

import java.util.TreeMap;
import java.util.Map;

public class TaskManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage tasks
        TreeMap<String, String> tasks = new TreeMap<>();

        // Adding tasks to the TreeMap
        tasks.put("Complete report", "Low");
        tasks.put("Schedule meeting", "Medium");

        // Adding an urgent task to the beginning using putFirst
        tasks.putFirst("Resolve server issue", "High");

        // Printing the tasks
        System.out.println("Tasks: ");
        for (Map.Entry<String, String> entry : tasks.entrySet()) {
            System.out.println(entry.getKey() + " - Priority: " + entry.getValue());
        }
    }
}

Output:

Tasks:
Resolve server issue - Priority: High
Complete report - Priority: Low
Schedule meeting - Priority: Medium

In this example, TreeMap.putFirst() is used to ensure that urgent tasks are given priority by placing them at the beginning of the map.

Conclusion

The TreeMap.putFirst() method introduced in Java 21 provides a way to add key-value pairs to the beginning of a TreeMap. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications while prioritizing certain entries. This method enhances the versatility of TreeMap by allowing specific entries to be positioned at the beginning, 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