Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Copy Key-Value Pairs from One TreeMap to Another in Java?
Next article icon

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 Java

To 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);     } } 

Output
Course 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


Next Article
How to Copy Key-Value Pairs from One TreeMap to Another in Java?

P

pranay0911
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Collections
  • Java-Library
  • java-TreeMap
  • Java Examples
Practice Tags :
  • Java
  • Java-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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences