I'm playing around with maps and sets and I've come across something which I cannot do. I'm trying to get the map interestsFans so it looks like:-
"Programming" { "Peter", "Luke" }
"Cycling" { "Peter", "Thomas" }
"Porno" { "Thomas", "Luke" }
ETC...
In particular the problem is:-Code:import java.util.*;
public class Maps
{
public static void mapPlay()
{
Map<String, Set<String>> personInterests = new HashMap<String, Set<String>>();
Set<String> allInterests = new HashSet<String>();
Set<String> allPeople = new HashSet<String>();
Map<String, Set<String>> interestFans = new HashMap<String, Set<String>>();
Set<String> interests = new HashSet<String>();
interests.add("Programming");
interests.add("Cycling");
interests.add("Walking");
interests.add("Cinema");
personInterests.put("Peter", interests);
interests = new HashSet<String>();
interests.add("Cinema");
interests.add("Cycling");
interests.add("Eating");
interests.add("Porno");
personInterests.put("Thomas", interests);
interests = new HashSet<String>();
interests.add("Porno");
interests.add("Eating");
interests.add("Programming");
interests.add("Walking");
personInterests.put("Luke", interests);
/**
* Populates the set referenced by allPeople with all
* the people in PersonInterests. Also populates the
* set referenced by allInterests with all the interests in
* personInterests.
*/
allPeople = personInterests.keySet();
for (String eachSetValue : personInterests.keySet())
{
allInterests.addAll(personInterests.get(eachSetValue) );
}
System.out.println("personInterests: " + personInterests + " ");
System.out.println("allPeople: " + allPeople + " ");
System.out.println("allInterests: " + allInterests + " ");
/**
* Works out which people likes which interests and stores
* this information in a map. interests represent
* the map key and which people like the interests
* represent the map values.
*/
for (String eachInterest : allInterests)
{
interestFans.put(eachInterest, new TreeSet<String>());
}
System.out.println("InterestFans: " + interestFans + " ");
for (String eachPerson : allPeople)
{
for (String eachInterest : personInterests.get(eachPerson))
{
System.out.println("eachPerson " + eachPerson);
System.out.println("eachInterests " + eachInterest);
// ** HOW TO ADD THE PERSON NAME TO THE VALUE SET
// ** OF THE MAP interestFans CORRESPONDING TO CORRECT
// ** KEY IN interestFans ??
}
}
}
}
I hope it makes sense what I'm trying to accomplish. Any help really appreciated.Code:for (String eachPerson : allPeople)
{
for (String eachInterest : personInterests.get(eachPerson))
{
System.out.println("eachPerson " + eachPerson);
System.out.println("eachInterests " + eachInterest);
// ** HOW TO ADD THE PERSON NAME TO THE VALUE SET
// ** OF THE MAP interestFans CORRESPONDING TO CORRECT
// ** KEY IN interestFans ??
}
}

