Results 1 to 4 of 4

Thread: Maps and Sets

  1. #1
    RedKMan is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Unhappy Maps and Sets

    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...


    Java 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 ??
                             
             }
          }
          
          
       }
       
    }
    In particular the problem is:-

    Java 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 ??
                             
             }
          }
    I hope it makes sense what I'm trying to accomplish. Any help really appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by RedKMan View Post
    I hope it makes sense what I'm trying to accomplish. Any help really appreciated.
    No, I'm sorry, it isn't clear to me; can you say it again please, but in words?

    kind regards,

    Jos

  3. #3
    RedKMan is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    OK,

    At the moment the map,

    Map<String, Set<String>> interestFans = new HashMap<String, Set<String>>();

    looks like,

    "Programming", { }
    "Cycling", { }
    etc...

    With the code below I am trying to work out an add statement that will see which person likes programming, which person likes cycling etc. and add their name to the set corresponding to that interest.

    E.G. Peter likes Programming, so add the name Peter to the set in interestFans whose key is Programming. Repeat this for all the names in the set allPeople and the all that persons interests in the map personInterests.

    Does this help?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by RedKMan View Post
    OK,

    At the moment the map,

    Map<String, Set<String>> interestFans = new HashMap<String, Set<String>>();

    looks like,

    "Programming", { }
    "Cycling", { }
    etc...

    With the code below I am trying to work out an add statement that will see which person likes programming, which person likes cycling etc. and add their name to the set corresponding to that interest.

    E.G. Peter likes Programming, so add the name Peter to the set in interestFans whose key is Programming. Repeat this for all the names in the set allPeople and the all that persons interests in the map personInterests.

    Does this help?
    Yep, now I understand; your map has an interest as a key and a Set of persons as its value; this map only allows you to find the Set of persons given an interest key. Why don't you build another map where the persons are the key and their interests are a Set as the corresponding value? Both maps will be a Map<String, Set<String>>.

    kind regards,

    Jos

Similar Threads

  1. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 04:07 PM
  2. Comparing List of maps
    By thorne_ in forum New To Java
    Replies: 1
    Last Post: 06-10-2009, 02:30 AM
  3. Maps
    By natep67 in forum New To Java
    Replies: 8
    Last Post: 05-06-2009, 03:59 AM
  4. How do I Link data of two maps toghther?
    By javanoobie in forum New To Java
    Replies: 13
    Last Post: 04-13-2009, 05:37 PM
  5. Google Maps API
    By mew in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 10:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •