Question Index

Write a program to override smaller length values from one map to another. If the keys do not exist in the destination map, copy them.

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=>"try"} {key4=>"Do", key3=>"not", key6=>"stupid", key2=>"try", "key1=>"play", key7=>"not"}
{key4=>"Do", key3=>"so", key6=>"open", key34=>"brains", key2=>"Your", key1=>"fall"}, {key4=>"not", key7=>"be", key6=>"NotOut", key2=>"minded", key3=>"brains"} {key4=>"not", key3=>"brains", key6=>"NotOut", key34=>"me", key2=>"minded", key1=>"fall", key7=>"be"}
{key10=>"Fun", key3=>"there", key6=>"fair", key2=>"set"}, {key1=>"move", key10=>"there", key3=>".", key9=>"up"} {key10=>"there", key3=>"there", key6=>"fair", key2=>"set", key1=>"move", key9=>"up"}

class OverrideSmallerLengthValuesFromOtherMap

{    public static void main(String s[])
        Map destination = new HashMap();
        destination.put("key11", "Hai");
        destination.put("key12", "how");
        destination.put("key13", "you");
        destination.put("key23", "xyz");
        destination.put("key14", ".");
        Map source = new HashMap();
        source.put("key21", "are");
        source.put("key12", "Hello");
        source.put("key22", "how");
        source.put("key23", "friend");
        source.put("key24", ".");
        source.put("key25", "Hello");
        overrideSmallerLengthValues(destination, source);
        System.out.println("The modified map is : " + destination);


public static void overrideSmallerLengthValues(Map<String, String> destination, Map<String, String> source) {
//Write code here to override the values of first map if key is present in the other map.



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