How to Copy Key-Value Pairs from One TreeMap to Another in Java? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In Java, a TreeMap is a Map implementation that stores key-value pairs in a red-black tree structure. It allows insertions and deletions of key-value pairs due to its tree implementation. These operations take O(log n) time on average. In this article, we will be learning how to copy key-value pairs from one TreeMap to another in Java. Syntax:newTreeMap.putAll(originalTreeMap);This will copy all the key-value pairs from originalTreeMap to newTreeMap. Program to Copy Key-Value Pairs from One TreeMap to Another in JavaTo copy key-value pairs from one TreeMap to another, we can use putAll() method. Below is the code implementation for this: Java // Java program to copy key-value pairs from one TreeMap to another using putAll() import java.util.TreeMap; public class CopyTreeMap { public static void main(String[] args) { // Create the course TreeMap TreeMap<String, Integer> courseTreeMap = new TreeMap<>(); courseTreeMap.put("Core Java", 10000); courseTreeMap.put("Spring Boot", 20000); courseTreeMap.put("AWS", 25000); // Print the course TreeMap System.out.println("Course TreeMap: " + courseTreeMap); // Create the another TreeMap TreeMap<String, Integer> newCourseTreeMap = new TreeMap<>(); // Copy key-value pairs using putAll() method newCourseTreeMap.putAll(courseTreeMap); // Print the newCourse TreeMap System.out.println("New Course TreeMap: " + newCourseTreeMap); } } OutputCourse TreeMap: {AWS=25000, Core Java=10000, Spring Boot=20000} New Course TreeMap: {AWS=25000, Core Java=10000, Spring Boot=20000} Explanation of the above Program:In the above program, it creates an original TreeMap with course names and fees as keys and values.A new empty TreeMap is created to copy the data to.We have used the putAll() method to copy all key-value pairs from the original map to the new map.This copies the entire contents of one map to another map in one line.Printing both maps verifies the data is successfully copied from one to the other.Note: putAll() method to easily copy all elements from one TreeMap to another in a single line of code Comment P pranay0911 Follow 0 Improve P pranay0911 Follow 0 Improve Article Tags : Java Java Programs Java-Collections Java-Library java-TreeMap Java Examples +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like