Question Index

Write a program to copy the all the keys and values from another map.

Input (HashMap, HashMap) Output (HashMap)
{key4=>"Hello", key3=>"are", key7=>"."}, {key5=>"how", key6=>"are", key2=>"you"} {key4=>"Hello", key3=>"are", key7=>".", key5=>"how", key6=>"are", key2=>"you"}
{key4=>"Do", key3=>"not", key6=>"stupid", key2=>"me"}, {key1=>"play", key6=>"with", key7=>"not", key2=>"it"} {key4=>"Do", key3=>"not", key6=>"with", key2=>"it", key1=>"play", key7=>"not"}
{key4=>"Do", key3=>"so", key6=>"open", key34=>"brains", key2=>"Your", key1=>"fall"}, {key4=>"not", key7=>"be", key6=>"out", key2=>"minded", key3=>"brains"} {key4=>"not", key3=>"brains", key6=>"out", key34=>"me", key2=>"minded", key1=>"fall", key7=>"be"}
{key10=>"Fun", key3=>"there", key6=>"fair", key2=>"set"}, {key1=>"they", key10=>"there", key3=>".", key9=>"up"} {key10=>"there", key3=>".", key6=>"fair", key2=>"set", key1=>"they", key9=>"up"}

import java.util.*;
class CopyAllKeysAndValuesFromAnotherMap
    public static void main(String s[])
        Map<String, String> destination = new HashMap<String, String>();
        destination.put("key11", "Hai");
        destination.put("key12", "how");
        destination.put("key13", "you");
        destination.put("key23", "xyz");
        destination.put("key14", "black");
        Map<String, String> source = new HashMap<String, String>();
        source.put("key21", "are");
        source.put("key13", "me");
        source.put("key12", "Hello");
        source.put("key22", "how");
        source.put("key23", "friend");
        source.put("key24", ".");
        copyAllKeysAndValues(destination, source);
        System.out.println("The modified map is : " + destination);


    public static void copyAllKeysAndValues(Map<String, String> destination, Map<String, String> source) {




Topic: Java HashMap Implementation

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App