Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: way too hard to explain this in short. please help guys!!

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default way too hard to explain this in short. please help guys!!

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

    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) );
          }
     
     
     
          /**
           * 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>());
          }
     
          for (String eachPerson : allPeople)
          {
             for (String eachInterest : personInterests.get(eachPerson))
             {
     
                // ** HOW TO ADD THE PERSON NAME TO THE VALUE SET
                // ** OF THE MAP interestFans CORRESPONDING TO CORRECT
                // ** KEY IN interestFans ??
     
             }
          }
     
     
       }
     
    }


    The part im actually having problems with is:

    for (String eachPerson : allPeople)
          {
             for (String eachInterest : personInterests.get(eachPerson))
             {
     
     
                // ** HOW TO ADD THE PERSON NAME TO THE VALUE SET
                // ** OF THE MAP interestFans CORRESPONDING TO CORRECT
                // ** KEY IN interestFans ??
     
             }


    the specifications are:

    for each person in allPeople
    For each interest in that person’s personInterests
    add the person to the set which is the value of that interest in personInterests

    im sure lots of you will say you dont understand, and newither do I. I have read over this so many times and it just ties my brain in knots!! im very close to completing the question but I just cant finish it. Any help is much appreciated as I really want to understand this!

    Thankyou in advance to anyone able and willing to help.
    Last edited by humdinger; March 11th, 2010 at 04:19 PM.


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: way too hard to explain this in short. please help guys!!

    for (String eachPerson : allPeople)
    {
     for (String eachInterest : personInterests.get(eachPerson))
    {
     
    this.allPeople.add(eachPerson);
    for (String eachInterest : personInterests.get(eachPerson))
    {
       this.allPeople.add(eachInterest );
    }
    I thinks
    Last edited by kyuss; March 13th, 2010 at 08:02 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: way too hard to explain this in short. please help guys!!

    shoudn the last line be:

    this.personInterests.add(eachInterest );

    ??

  4. The Following User Says Thank You to davie For This Useful Post:

    kyuss (March 13th, 2010)

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: way too hard to explain this in short. please help guys!!

    I haven't gone through much of your code to actually solve the asked problem (sorry), but if I understand the problem and code correctly it seems easier to write a specific object - such as Person - wrapping a set in that object to define interests, and if you want to find all the people with common interests create a look up table using a Map - adding Person to a List/Set keyed with the interest (make sure in doing so to override the equals() and hashcode() methods of any custom object placed in a collection like Set or Map)

  6. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: way too hard to explain this in short. please help guys!!

    Hi there, I am working on this question and I'm well and truly stuck on this part:
    • For each band stored in allBands create an entry in bandFans with the band
    name as key and a new empty set as its value.

    • For each person in allPeople

    o For each band in that person’s personInterests

    add the person to the set which is the value of that band in
    bandFans.
    The first two points are working okay (I think) but I just can't seem to figure out how to add the all names into the set (only one currently). This is my code so far:
       public void workOutBandFans()
       {
          Set<String>peopleSet = new HashSet<String>();
          for (String band : allBands)
          {
           bandFans.put(band, new HashSet<String>());
          }
          for (String person : allPeople)
          {
             for (String band : personInterests.get(person))
             {
                peopleSet.add(person);
                bandFans.put(band, peopleSet);
     
             }
     
             peopleSet = new HashSet<String>();
          }