Question Index

Write a program to override the values in destination map only if key is present. If the key is not present in the destination map, do not copy them from the source map.

Input (HashMap, HashMap) Output (HashMap)
{key4=>"Hello", key3=>"are", key7=>"."}, {key5=>"how", key6=>"are", key2=>"you"} {key4=>"Hello", key3=>"are", key7=>"."}
{key4=>"Do", key3=>"not", key6=>"stupid", key2=>"me"}, {key1=>"play", key6=>"with", key7=>"not", key2=>"it"} {key4=>"Do", key3=>"not", key6=>"with", key2=>"it"}
{key4=>"Do", key3=>"so", key6=>"open", key2=>"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"}
{key10=>"Fun", key3=>"there", key6=>"fair", key2=>"set"}, {key1=>"move", key10=>"there", key3=>".", key9=>"up"} {key10=>"there", key3=>".", key6=>"fair", key2=>"set"}

import java.util.*;
class OverrideValuesFromAnotherMap
    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", ".");
        Map<String, String> source = new HashMap<String, String>();
        source.put("key21", "are");
        source.put("key12", "Hello");
        source.put("key22", "how");
        source.put("key23", "friend");
        source.put("key24", ".");
        source.put("key25", "Hello");
        overrideExistingKeys(destination, source);
        System.out.println("The modified map is : " + destination);


    public static void overrideExistingKeys(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