Question
Write a Java method called intersectingHobbies that takes a hobby and a map with keys that are TA names and values that are a list
Write a Java method called intersectingHobbies that takes a hobby and a map with keys that are TA names and values that are a list of favorite hobbies for that TA and that returns a set that contains all the hobbies of the TAs that like the given hobby. The hobby map uses TA names as keys (strings) and has sets of hobby names as values (also strings).
For example, a variable called hobbies might contain the following map:
{Leah=[reading, hiking], Rinav=[running, piano, reading], Melody=[music, cooking, knitting]}
This map indicates that Leah likes reading and hiking and that Rinav likes running, piano, and reading, etc. If the following call is made:
Set
then the variable result would be set to the following set:
[hiking, piano, reading, running]
Notice that the method returns a set containing all the hobbies liked by TAs who like reading. In the example above, Leah and Rinav like reading. Therefore, the method returns a set containing all of the hobbies those TAs like, including reading itself.
Another way of thinking about what this method is doing is that each time it encounters a TA that likes the target hobby, it includes all of the hobbies that TA likes in the overall answer.
The set returned by the method should be ordered alphabetically. If no TA likes the given hobby, the method should return an empty set.
You may assume that the given string and map are not null and none of the map's keys or values are null or reference null elements. The method should not modify the provided map or any of the structures it references. Note that you are limited to the methods provided on the cheat sheet.
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