BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

how to copy one hashmap content to another hashmap

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we are gonna learn how to copy one HashMap elements to another HashMap. We will be using putAll() method of HashMap class to perform this operation. Complete code as follows:

import java.util.HashMap;
class HashMapDemo{ 
  public static void main(String[] args) {
     // Create a HashMap
     HashMap<Integer, String> hmap = new HashMap<Integer, String>();
 
     //add elements to HashMap
     hmap.put(1, "AA");
     hmap.put(2, "BB");
     hmap.put(3, "CC");
     hmap.put(4, "DD");
 
     // Create another HashMap
     HashMap<Integer, String> hmap2 = new HashMap<Integer, String>();
 
     // Adding elements to the recently created HashMap
     hmap2.put(11, "Hello");
     hmap2.put(22, "Hi");
 
     // Copying one HashMap "hmap" to another HashMap "hmap2"
     hmap2.putAll(hmap);
 
     // Displaying HashMap "hmap2" content
     System.out.println("HashMap 2 contains: "+ hmap2);
  }
}

Output:

HashMap 2 contains: {1=AA, 2=BB, 3=CC, 4=DD, 22=Hi, 11=Hello}

All the elements of HashMap 1 got copied to the HashMap 2. putAll() operation does not replace the existing elements of Map rather it appends the elements to them.

Top Related Articles:

  1. How to loop LinkedList in Java
  2. How to synchronize HashMap in Java with example
  3. Difference between HashSet and TreeSet
  4. Java – LinkedList peek(), peekFirst() and peekLast() methods
  5. How to sort HashMap in Java by Keys and Values

Tags: Collections, Java-HashMap

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Tanu says

    May 1, 2015 at 1:05 AM

    The following lines should be:

    / Adding elements to the recently created HashMap
    hmap2.put(11, “Hello”);
    hmap2.put(22, “Hi”);

    instead of the following:
    // Adding elements to the recently created HashMap
    hmap.put(11, “Hello”);
    hmap.put(22, “Hi”);

    I know this is a very small thing, but as your post says it is for beginner’s I guess everything should be correct.

    Reply
    • Chaitanya Singh says

      May 1, 2015 at 7:55 AM

      Thanks for pointing that out. It was a typo, I have fixed it.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap