Write a method that, given two maps, yields a map with all key/value associations that occur...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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; } 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; }
Expert Answer:
Related Book For
General Chemistry Principles And Modern Applications
ISBN: 9780132931281
11th Edition
Authors: Ralph Petrucci, Jeffry Madura, F. Herring, Carey Bissonnette
Posted Date:
Students also viewed these programming questions
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
Explain informally the difference between Godel's completeness theorem and his first incompleteness theorem. [8 marks] (b) State the meaning of Hoare triples {P} C {Q} in separation logic. [3 marks]...
-
Determine the degrees of freedom under the following conditions: (a) Tl-20 wt% Pb at 325 C and 400 C; (b) Tl-40 wt% Pb at 325 C and 400 C; (c) Tl-90 wt% Pb at 325 C and 400 C. Refer to the phase...
-
In an application involving 30 d.f. you obtained a t statistic of 0.68. Since this t value is not statistically significant even at the 10% level of significance, you can safely accept the relevant...
-
Discuss the economic benefits for primary and secondary markets. Briefly explain your understanding of an IPO, the net valuation of a firm and what can happen once the shares are traded on secondary...
-
Analyze the main reasons for HRIS implementation failure. How can we prevent these from affecting us?
-
Given the following diagram, a. What is the capacity of this system? b. If the capacity of one operation could be increased in order to increase the output of the system, which operation should it...
-
Based on positive results in DT&E and following a successful Operational Test Readiness Review, the SRAW entered IOT&E. The IOT&E plan called for three phases with the first phase (20 shots) being...
-
If we obtain a personal loan of $8,000 for 48 months, with an annual rate of 21% which we plan to amortize through unpaid balances, what is the first monthly payment that the bank will deduct from us...
-
1. Explain three (3) key principles of consumer behavior and their influences on consumer buying behavior. 2. Explain the four (4) types of consumer buying behavior. 3. Explain each key stage in...
-
Write a C++ program that displays a message as "Welcome to CS 280 in Spring 2023 Semester", and prompts the user to enter his/her first name and his/her last name. The program should read the user's...
-
Explore the complexities of inter-process communication mechanisms in contemporary operating systems, including shared memory, message passing, and synchronization primitives.
-
Discuss the nuances of demand paging in virtual memory systems, detailing the mechanisms involved in page fault handling, page replacement algorithms, and their impact on overall system...
-
A 20.00 kg model plane started at rest on the ground. After takeoff, it was flying at a height of 35.0 m with a speed of 30 m/s. If 25,000 J of work was done, how much energy was wasted and where did...
-
Respond that the following statements are TRUE or FALSE: 1) A standard is a price, cost, or quantity that is expected under normal conditions. 2) A standard cost system is an accounting system that...
-
D Which of the following is considered part of the Controlling activity of managerial accounting? O Choosing to purchase raw materials from one supplier versus another O Choosing the allocation base...
-
What are the types of interpersonal communication?
-
How does one choose between communication methods and handle barriers to effective communication?
-
What are the various forms of virtual communication used in modern organizations?
Study smarter with the SolutionInn App