Results 1 to 20 of 20

Thread: Maps and Sets

  1. #1
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default Maps and Sets

    Hi All

    I have Map:

    Java Code:
          bandFans = new HashMap<String, Set<String>>();
    for instance:

    BandFans
    <Oasis,<Tom, Jerry, Martin>>
    <SpiceGirls,<Suzy, Ella>>
    <Nirvana, <Martin, Jack, Victor, Kelly>>

    I'm writing function that will determine the bands that have the most fans:

    Java Code:
        /**
        * It work out the most popular band. 
        */  
       public void getMostPopularBands()
       {
          int mostFans = 0;
         for( Set<String> someSet : bandFans.values())
         {
            if (someSet.size() > mostFans)
            {
               mostFans = someSet.size();
             }
             
          }
    
         for( Set<String> someSet : bandFans.values())
         {
            if (someSet.size() == mostFans)
            {
               System.out.println(bandFans.get(someSet));
             }         
          }   
    }
    First part of the method works fine:
    The value stored in mostFans is the value of the higher number of fans for one band in entire map

    Second part should compare mostFans value with number of fans of each band and if equal - display name of that band. But I cant make that work. Compiles just fine but when run it returns value: null

    Can anyone please point me to where the error is?

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

    Default

    You can only get(key) from a Map; if the key is stored in the Map it returns the associated value. You are trying to get(value) (i.e. a Set) from the Map; of course it doesn't contain any associated value so it returns null. Iterate over the keyset of the Map; it is an iterable of Map.Entry elements; every Entry contains a getKey() as well as a getValue() method which should solve your problem.

    kind regards,

    Jos

  3. #3
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Hi

    Thanks! I am iterateing over the keyset of the Map now but it dosent compile cause method size() dosent work. Have look what I have changed:

    Java Code:
        /**
        * It work out the most popular band. 
        */  
       public void getMostPopular()
       {
          int mostFans = 0;
         for( Set<String> someSet : bandFans.values())
         {
            if (someSet.size() > mostFans)
            {
               mostFans = someSet.size();
             }
             
          }
    
          
          for( String someBand : bandFans.keySet())
        {
             int tempSize = [COLOR="Red"]someBand.size();[/COLOR] // error is here - cannot find method size()
             if (bandFans.someBand.size() == mostFans)
            {
             System.out.println(bandFans.get(SomeBand));
              }
          }
    }

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

    Default

    Quote Originally Posted by darkblue24 View Post
    Hi

    Thanks! I am iterateing over the keyset of the Map now but it dosent compile cause method size() dosent work. Have look what I have changed:
    Sorry, my mistake; I wanted to type entryset, not keyset, but my fingers thought otherwise; you can iterate over such a set and it gives you Map.Entry items; each item has a getKey() and getValue() method.

    kind regards,

    Jos

  5. #5
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Hi Jos

    Do you mean valueset? Cause word entryset is not recognised by compiler. Thats hat I have now and it dosnt compiles:

    Java Code:
          for( String someBand : bandFans.entryset)
        {
             int tempSize = someBand.size();
             if (bandFans.someBand.size() == mostFans)
            {
             System.out.println(bandFans.get(SomeBand));
              }
          }

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

    Default

    Quote Originally Posted by darkblue24 View Post
    Hi Jos

    Do you mean valueset? Cause word entryset is not recognised by compiler. Thats hat I have now and it dosnt compiles:

    Java Code:
          for( String someBand : bandFans.entryset)
        {
             int tempSize = someBand.size();
             if (bandFans.someBand.size() == mostFans)
            {
             System.out.println(bandFans.get(SomeBand));
              }
          }
    Read the API documentation for the Map interface; the method is entrySet() and it gives you a Set of Map.Entry type objects.

    kind regards,

    Jos

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    You really ought to use the API docs...they're quite handy.

    But, with entrySet(), you should be able to do this in a single loop, saving the Entry for the band with the most members.

  8. #8
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    entryset()cReturns a Set view of the mappings contained in this map.

    how can that help me?
    what Im trying to do is to compare size() of each set with value of mostFans and if true print out key value coresponding to that set.

    How can I use entryset to do that?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    The Entry returned from the EntrySet has the Key (ie the bandname) and the Value (ie the Set of fans), so you can simply compare the size of the Set of fans in the Entry with the previous largest fan base (that is the Entry that's previously been set aside), if it's bigger then that becomes the new largest band...if the first time through then it will automatically be the new largest.

    So, one loop.

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

    Default

    Quote Originally Posted by darkblue24 View Post
    entryset()cReturns a Set view of the mappings contained in this map.

    how can that help me?
    what Im trying to do is to compare size() of each set with value of mostFans and if true print out key value coresponding to that set.

    How can I use entryset to do that?
    Here, this is a spoiler:
    Java Code:
    for (Map.Entry<String, Set<String>> entry: yourMap.entrySet()) {
       String key= entry.getKey();
       Set<String> value= entry.getValue();
    
       if (value.size() == maximumSize)
          ...
    }
    no comments included because this is a spoiler. You could've found it yourself if you had studied the API documentation for those interfaces.

    kind regards,

    Jos

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    And if you held onto an Entry<String,Set<String>> (called biggestBand for example) instead of a maximumSize, you could do

    Java Code:
    if (value.size() > biggestBand.getValue().size()) {...}
    in the first loop...not that I'm obsessed or anything.
    :)

  12. #12
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    I had look on the EntrySet and I tried to apply to my method but it is just impossible for me to use it - it is outside of the scope of the course that I doing. I have spend one week doing this assignment and created several methods - this is the last method that will complete all my work but I just cant make it work.

    No one ever mentioned about EntrySet in course work so must be onther way of doing it.

    Can I use 'union' 'intersection' or 'differemce' code to do that?
    OR maybe if you have an example of using EntrySet - similar to my case that I could adapt?

    Java Code:
          for( Map bandFans : bandFans.entrySet())
        {
             int tempSize = someBand.size();
             if (bandFans.someBand.size() == mostFans)
            {
             System.out.println(bandFans.get(SomeBand));
              }
          }

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

    Default

    Quote Originally Posted by darkblue24 View Post
    I had look on the EntrySet and I tried to apply to my method but it is just impossible for me to use it - it is outside of the scope of the course that I doing. I have spend one week doing this assignment and created several methods - this is the last method that will complete all my work but I just cant make it work.

    No one ever mentioned about EntrySet in course work so must be onther way of doing it.

    Can I use 'union' 'intersection' or 'differemce' code to do that?
    OR maybe if you have an example of using EntrySet - similar to my case that I could adapt?

    Java Code:
          for( Map bandFans : bandFans.entrySet())
        {
             int tempSize = someBand.size();
             if (bandFans.someBand.size() == mostFans)
            {
             System.out.println(bandFans.get(SomeBand));
              }
          }
    You didn't even read my reply#10 did you?

    kind regards,

    Jos

  14. #14
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Hey guys!

    It works! Oh thank you soooo much!!!!! Now the whole program works fine!
    Thank you!!!!

  15. #15
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    JosAH your post helped me most - how can I add positive points to your score (reputation) on here?

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

    Default

    Quote Originally Posted by darkblue24 View Post
    JosAH your post helped me most - how can I add positive points to your score (reputation) on here?
    I really wouldn't know; for one thing I never posted a question here so I've never seen the dashboard with the options to 'rep' anything. Maybe you can ask a moderator (I've seen Fubarable around just now).

    kind regards,

    Jos
    Last edited by JosAH; 03-25-2010 at 05:21 PM.

  17. #17
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Found it! thnx again

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

    Default

    Quote Originally Posted by darkblue24 View Post
    Found it! thnx again
    You're welcome of course; so what happened now? I don't feel any richer or more beautiful than I already was ... ;-)

    kind regards,

    Jos

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    You have the undying admiration of the rest of us...no...honestly...

    ;)

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

    Default

    Quote Originally Posted by Tolls View Post
    You have the undying admiration of the rest of us...no...honestly...

    ;)
    Does that include cookies (with chocolate!) and beer? ;-)

    kind regards,

    Jos

Similar Threads

  1. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 09:36 AM
  2. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 04:07 PM
  3. Comparing List of maps
    By thorne_ in forum New To Java
    Replies: 1
    Last Post: 06-10-2009, 02:30 AM
  4. Maps
    By natep67 in forum New To Java
    Replies: 8
    Last Post: 05-06-2009, 03:59 AM
  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
  •