Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a method that, given two maps, yields a map with all key/value associations that occur in either map. In the resulting map, a
Write a method that, given two maps, yields a map with all key/value associations that occur in either map. In the resulting map, a key is associated with the set (of size 1 or 2) containing the values to which the key is mapped in the given maps. Use a TreeMap so that the values will appear in order. import java.util.Map; import java.util.HashMap; public class MergedTester { Maps.java public static void main(String[] args) { MergedTester.java Map a = new HashMap (); Map b = new HashMap (); Map c = new HashMap (); a.put("Jack", 1234); a.put("Jill", 4567); a.put("Alice", 1222); b.put("Jack", 777); b.put("Jill", 4567); b.put("Alice", 122); } } c.put("Rodney", 4567); c.put("Alice", 1222); c.put("Jack", 777); c.put("Jill", 4567); System.out.println(Maps.merge(a, b)); System.out.println( "Expected: {Alice=[122, 1222], Jack=[777, 1234], Jill=[4567]}"); System.out.println(Maps.merge(a, c)); System.out.println( "Expected: {Alice=[1222], Jack=[777, 1234], Jill= [4567], Rodney= [4567]}"); System.out.println(Maps.merge(c, b)); System.out.println( "Expected: {Alice=[122, 1222], Jack=[777], Jill=[4567], Rodney= [4567]}"); Write a method that, given two maps, yields a map with all key/value associations that occur in either map. In the resulting map, a key is associated with the set (of size 1 or 2) containing the values to which the key is mapped in the given maps. Use a TreeMap so that the values will appear in order. Maps.java MergedTester.java 1 import java.util.Map; 2 import java.util.Set; 3 import java.util.TreeSet; 4 import java.util. TreeMap; 5 public class Maps 6 { 7 8 9 10 11 12 /** Returns a new map with merged key-value pairs. @param map1 the first map to compare @param map2 the second map to compare @return a map containing a set of entries in each map public static Map merge(Map map1, Map map2) 13 14 15 { Write a method that, given two maps, yields a map with all key/value associations that occur in either map. In the resulting map, a key is associated with the set (of size 1 or 2) containing the values to which the key is mapped in the given maps. Use a TreeMap so that the values will appear in order. Maps.java Merged Tester.java 8 9 @param map1 the first map to compare 10 @param map2 the second map to compare 11 12 13 14 Returns a new map with merged key-value pairs. @return a map containing a set of entries in each map public static Map merge(Map map1, Map map2) 15 { 16 Map merged = new TreeMap (); 17 18 *Your code goes here */ 19 20 21 222 22 } return merged; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started