How to Copy Key-Value Pairs from One TreeMap to Another in Java? Last Updated : 06 Feb, 2024 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 More infoAdvertise with us Next Article How to Copy Key-Value Pairs from One TreeMap to Another in Java? P pranay0911 Follow Improve Article Tags : Java Java Programs Java-Collections Java-Library java-TreeMap Java Examples +2 More Practice Tags : JavaJava-Collections Similar Reads How to Create a TreeMap in Java and Add Key-Value Pairs in it? In Java, a TreeMap maintains elements in a sorted order as it is an implementation of the SortedMap Interface. It stores key-value pairs in a sorted order. In this article, we will explore the creation of TreeMap in Java and learn the step-by-step process of adding key-value pairs. Program to Create 2 min read How to Sort a TreeMap By Value in Java? In Java Language, a TreeMap always stores key-value pairs which are in sorted order on the basis of the key. TreeMap implements the NavigableMap interface and extends AbstractMap class. TreeMap contains unique keys. Sorting TreeMap by value in Java The elements in TreeMap are sorted on the basis of 3 min read How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? The Java Collections Framework includes the TreeMap class, in which Java offers a sorted collection of key-value pairs. By default, TreeMap uses a custom Comparator or arranges components according to their natural ordering. In this article, we will learn how to apply a custom order for key-value pa 2 min read How to Add Key-Value pairs to LinkedHashMap in Java? LinkedHashMap is a Hash table and linked list implementation of the Map interface. In LinkedHashMap order of key-value pair depends on the order in which keys were inserted into the map. Insertion order does not affect if a key is reinserted into the map. Example: Input: Key: 1 Value : 1221 Key: 2 V 2 min read How to Get TreeMap Key or Value using Index in Java? The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using whic 5 min read Like