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;
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
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;
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F26e509f6-d35a-44c9-95ba-d273bdf29c4f%2Fccf1ef99-b75c-4f74-b692-a52c90dba9da%2F9lah9ne_processed.jpeg&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education