How to Merge Two TreeMaps in Java? Last Updated : 23 Jul, 2025 Suggest changes Share 2 Likes Like Report In Java, TreeMap is a pre-defined class that implements the NavigableMap interface and extends the AbstractMap class. In this article, we will learn to Merge Two Tree Maps in Java. Program to Merge Two TreeMaps in JavaWe are using the putAll method is inherited from the AbstractMap class in this manner possible. All mappings between TreeMaps are replicated using this technique. put: Accept the two parameters are key and valueputAll: Accept the one parameter as a TreeMap objectBelow is the implementation to Merge Two TreeMaps in Java: Java // Java code to show the implementation of // putAll method in TreeMap class import java.io.*; import java.util.*; // Driver Class class GFG { // Main Function public static void main (String[] args) { // Creating the two treeMaps TreeMap<Integer, String> treeMap1 = new TreeMap<>(); TreeMap<Integer, String> treeMap2 = new TreeMap<>(); // Adding the values into the treeMap1 treeMap1.put(11,"Greek"); treeMap1.put(12,"for"); treeMap1.put(13,"Greeks"); // Adding the values into the treeMap2 treeMap2.put(14,"Merging"); treeMap2.put(15,"two treeMaps"); treeMap2.put(16,"example"); // Merge the TreeMaps using putAll treeMap1.putAll(treeMap2); // Print the Merged TreeMap System.out.println("Merged TreeMap:"+treeMap1); } } OutputMerged TreeMap:{11=Greek, 12=for, 13=Greeks, 14=Merging, 15=two treeMaps, 16=example} Explaination of the above Program:Inserting Elements in First TreeMap and then in another TreeMap. After that we can use putAll() to put all the elements in the TreeMap1 after using the statement mentioned below: TreeMap1.putAll(TreeMap2); M maheshkadambala Follow 2 Article Tags : Java Java Programs Java-Collections Java - util package 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 Java5 min readJava Comparator Interface6 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 Java10 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 My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like