Java TreeMap putAll() Method

The TreeMap.putAll() method in Java is used to copy all of the mappings from the specified map to this TreeMap.

Table of Contents

  1. Introduction
  2. putAll Method Syntax
  3. Examples
    • Copying Entries from Another Map
    • Merging Multiple Maps
  4. Real-World Use Case
    • Example: Consolidating Multiple Data Sources
  5. Conclusion

Introduction

The TreeMap.putAll() method is a member of the TreeMap class in Java. It allows you to copy all of the mappings from the specified map to the current TreeMap. If the specified map is empty, the TreeMap remains unchanged. The TreeMap class implements the SortedMap interface, ensuring that the keys are sorted in their natural order or according to a specified comparator.

putAll() Method Syntax

The syntax for the putAll method is as follows:

public void putAll(Map<? extends K, ? extends V> map)
  • The method takes one parameter:
    • map of type Map<? extends K, ? extends V>, which represents the map from which the mappings are to be copied.
  • The method does not return a value.

Examples

Copying Entries from Another Map

The putAll method can be used to copy all entries from another map to a TreeMap.

Example

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

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

        // Creating a HashMap with some entries
        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("Ravi", 25);
        hashMap.put("Priya", 30);
        hashMap.put("Vijay", 35);

        // Copying entries from the HashMap to the TreeMap
        treeMap.putAll(hashMap);

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

Output:

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

Merging Multiple Maps

The putAll method can also be used to merge entries from multiple maps into a TreeMap.

Example

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

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

        // Creating two HashMaps with some entries
        Map<String, Integer> map1 = new HashMap<>();
        map1.put("Ravi", 25);
        map1.put("Priya", 30);

        Map<String, Integer> map2 = new HashMap<>();
        map2.put("Vijay", 35);
        map2.put("Anita", 28);

        // Merging entries from both HashMaps into the TreeMap
        treeMap.putAll(map1);
        treeMap.putAll(map2);

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

Output:

TreeMap after merging: {Anita=28, Priya=30, Ravi=25, Vijay=35}

Real-World Use Case

Example: Consolidating Multiple Data Sources

A common real-world use case for TreeMap.putAll() is consolidating data from multiple sources. For instance, let’s consider consolidating employee records from different departments into a single TreeMap.

Example

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

public class EmployeeDataConsolidation {
    public static void main(String[] args) {
        // Creating a TreeMap to store consolidated employee data
        TreeMap<String, Integer> employeeData = new TreeMap<>();

        // Creating HashMaps for different departments with employee data
        Map<String, Integer> hrData = new HashMap<>();
        hrData.put("Ravi", 25);
        hrData.put("Priya", 30);

        Map<String, Integer> itData = new HashMap<>();
        itData.put("Vijay", 35);
        itData.put("Anita", 28);

        Map<String, Integer> financeData = new HashMap<>();
        financeData.put("Rajesh", 45);
        financeData.put("Neha", 29);

        // Consolidating data from all departments into the TreeMap
        employeeData.putAll(hrData);
        employeeData.putAll(itData);
        employeeData.putAll(financeData);

        // Printing the consolidated employee data
        System.out.println("Consolidated Employee Data: " + employeeData);
    }
}

Output:

Consolidated Employee Data: {Anita=28, Neha=29, Priya=30, Rajesh=45, Ravi=25, Vijay=35}

In this example, TreeMap.putAll() is used to consolidate employee records from different departments into a single sorted map, making it easy to manage and retrieve employee data in alphabetical order.

Conclusion

The TreeMap.putAll() method in Java provides a way to copy all of the mappings from a specified map to the current 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 copying of new pairs and the merging 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