What is wrong with the given method called intersect that is supposed to take two Maps of strings to integers as parameters and return a new map whose contents are the intersection of the two. The intersection of two maps is defined here as the set of keys and values that exist in both maps. So if some key K maps to value V in both the first and second maps, include it in your result. If K does not exist as a key in both maps, or if K does not map to the same value V in both maps, exclude that pair from your result. For example, consider the following two maps: {J=87, L-62, W-46, A-100, S-80, J-88, K-52, S-95} {L=62, K=52, W=52, J=88, S=80, B=60, L=83, s-87} Calling intersect on the preceding maps would return the following new map (the order of the key/value pairs does not matter): {L-62, S-80, J=88, K-52} Correct the following code by rewriting the correct answer in your textbox: public static Map intersect (Map m1, Map m2){ Set m2Set= new HashSet<>(m2.keySet());// get the keys of map 1 Set m1Set= new HashSet<>(m1.keySet());// get the keys of map 2 m1Set.retainAl1(m2Set); return m1;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java language

 

What is wrong with the given method called intersect that is supposed to take two Maps of strings to
integers as parameters and return a new map whose contents are the intersection of the two. The
intersection of two maps is defined here as the set of keys and values that exist in both maps. So if
some key K maps to value V in both the first and second maps, include it in your result. If K does not
exist as a key in both maps, or if K does not map to the same value V in both maps, exclude that pair
from your result. For example, consider the following two maps:
{J=87, L=62, W=46, A=100, S=80, J=88, K=52, S=95}
{L=62, K=52, W=52, J=88, S=80, B=60, L=83, S=87}
Calling intersect on the preceding maps would return the following new map (the order of the
key/value pairs does not matter):
{L=62, S-80, J=88, K=52}
Correct the following code by rewriting the correct answer in your textbox:
public static Map<String, Integer> intersect (Map<String, Integer> m1,
Map<String, Integer> m2){
Set<String> m2Set= new HashSet<>(m2.keySet());// get the keys of map 1
Set<String> m1Set= new HashSet<>(m1.keySet());// get the keys of map 2
m1Set.retainAll(m2Set);
return m1;
}
Transcribed Image Text:What is wrong with the given method called intersect that is supposed to take two Maps of strings to integers as parameters and return a new map whose contents are the intersection of the two. The intersection of two maps is defined here as the set of keys and values that exist in both maps. So if some key K maps to value V in both the first and second maps, include it in your result. If K does not exist as a key in both maps, or if K does not map to the same value V in both maps, exclude that pair from your result. For example, consider the following two maps: {J=87, L=62, W=46, A=100, S=80, J=88, K=52, S=95} {L=62, K=52, W=52, J=88, S=80, B=60, L=83, S=87} Calling intersect on the preceding maps would return the following new map (the order of the key/value pairs does not matter): {L=62, S-80, J=88, K=52} Correct the following code by rewriting the correct answer in your textbox: public static Map<String, Integer> intersect (Map<String, Integer> m1, Map<String, Integer> m2){ Set<String> m2Set= new HashSet<>(m2.keySet());// get the keys of map 1 Set<String> m1Set= new HashSet<>(m1.keySet());// get the keys of map 2 m1Set.retainAll(m2Set); return m1; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to computer system
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education