Java TreeMap put() Method

The TreeMap.put() method in Java is used to insert key-value pairs into a TreeMap.

Table of Contents

  1. Introduction
  2. put Method Syntax
  3. Examples
    • Adding Entries to a TreeMap
    • Handling Duplicate Keys
  4. Real-World Use Case
    • Example: Managing a Student Gradebook
  5. Conclusion

Introduction

The TreeMap.put() method is a member of the TreeMap class in Java. It allows you to add or update key-value pairs in a TreeMap. If the key is not already present in the map, the method inserts the new key-value pair and returns null. If the key is already present, the method updates the value associated with the key and returns the previous value. The TreeMap class implements the SortedMap interface, ensuring that the keys are sorted in their natural order or according to a specified comparator.

put() Method Syntax

The syntax for the put method is as follows:

public V put(K key, V value)
  • The method takes two parameters:
    • key of type K, which represents the key to be inserted or updated.
    • value of type V, which represents the value to be associated with the key.
  • The method returns the previous value associated with the key, or null if there was no mapping for the key.

Examples

Adding Entries to a TreeMap

The put method can be used to add key-value pairs to a TreeMap.

Example

import java.util.TreeMap;

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

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

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

Output:

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

Handling Duplicate Keys

The put method returns the previous 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> people = new TreeMap<>();

        // Adding entries to the TreeMap
        Integer previousValue1 = people.put("Ravi", 25);
        Integer previousValue2 = people.put("Priya", 30);
        Integer previousValue3 = people.put("Ravi", 40); // Updating the value for the key "Ravi"

        // Printing the results of adding entries
        System.out.println("Previous value for Ravi: " + previousValue1);
        System.out.println("Previous value for Priya: " + previousValue2);
        System.out.println("Previous value for Ravi after update: " + previousValue3);

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

Output:

Previous value for Ravi: null
Previous value for Priya: null
Previous value for Ravi after update: 25
TreeMap: {Priya=30, Ravi=40}

Real-World Use Case

Example: Managing a Student Gradebook

A common real-world use case for TreeMap is managing a student gradebook where student names are sorted in alphabetical order.

Example

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

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

        // Adding student grades to the TreeMap
        studentGrades.put("Ravi", "A");
        studentGrades.put("Priya", "B");
        studentGrades.put("Vijay", "A");
        studentGrades.put("Anita", "C");

        // Printing the student grades in sorted order
        System.out.println("Student Grades: ");
        for (Map.Entry<String, String> entry : studentGrades.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Student Grades:
Anita: C
Priya: B
Ravi: A
Vijay: A

In this example, TreeMap is used to maintain the student names in alphabetical order, making it easy to retrieve and display the grades in a sorted manner.

Conclusion

The TreeMap.put() method in Java provides a way to add or update key-value pairs in a TreeMap. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications while maintaining a sorted order. The method allows you to handle both the insertion of new pairs and the updating of existing pairs, making it a versatile tool for data management.

Leave a Comment

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

Scroll to Top