![]() |
VOOZH | about |
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.
newTreeMap.putAll(originalTreeMap);This will copy all the key-value pairs from originalTreeMap to newTreeMap.
To copy key-value pairs from one TreeMap to another, we can use putAll() method. Below is the code implementation for this:
Course TreeMap: {AWS=25000, Core Java=10000, Spring Boot=20000}
New Course TreeMap: {AWS=25000, Core Java=10000, Spring Boot=20000}
Note: putAll() method to easily copy all elements from one TreeMap to another in a single line of code