Results 1 to 11 of 11
Like Tree4Likes
  • 2 Post By pbrockway2
  • 1 Post By pbrockway2
  • 1 Post By Tolls

Thread: How to iterate through Map to find specific keys (based on the values) !?

  1. #1
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default How to iterate through Map to find specific keys (based on the values) !?

    So I have the following code in a constructor:

    Java Code:
    this.bookMap = new TreeMap<String, Set<Integer>>();
    The TreeMap will take a String (for names in a book), and Integers in a Set (for pages they occur in the book).

    I then fill up the Map with words and pages that the words occur on like so:

    Java Code:
    Key -> Value
    ==========
    "Earth" ->  {1, 2, 3}
    "Moon" -> {7, 9 15}
    "Star" -> {22, 17, 22}
    "Planet" -> {1, 6, 15}
    The problem:

    I am being asked to make a method that will return the words that occur in the book within a specified starting and ending page range (inclusive)

    I just have no idea where to start with this.

    I thought about something like:
    Java Code:
    int minPage = 5; // variable for minPage value
    int maxPage = 25; // variable for maxPage value
    
    for (int i = minPage; minPage =< maxPage; i++) // loop until it goes through every page between min and max value
         {
          for (eachKey : aBook.keySet()) // for each statement to run if above condition is true
               {
    
                Println("eachKey") // print each key that is in the valid range of pages
                }
         }

    I have spent the last couple of hours doing this and I just can't figure it out.
    I have tried looking all over google but I couldn't find anything that specific to help me with this.

    Hope you can help thanks.
    Last edited by javacakes; 05-07-2013 at 05:43 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Seems to me you might want to build a Map that goes the other way.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Well the specification was that the "words" need to be unique in the book so I assume it has to be a key.
    Then the values can contain multiple values, for pages, but has to be sorted. So I used a SortedSet.

    I just have no idea how to do this.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Loop over the entry set.
    For each entry loop over the values checking against the min and max. If at any point the value fits in the range, then add to your list of found words, and if at any point the value exceeds the max page then skip and continue to the next entry.
    Please do not ask for code as refusal often offends.

  5. #5
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Hi Tolls,

    How would I do this?

    The closest I can come to think is like something I wrote above but I still don't fully understand.

  6. #6
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Ok I think I got it except I get illegal start of expression on the If statement.
    Any ideas?

    Java Code:
     
    public Set<String> findNames(int startPage, int endPage) 
       {
           Set<String> aName = new HashSet<>(); 
          
           
           for (String eachKey : this.bookMap)
            {
                if (bookMap.get(eachKey) >= startPage && <= endPage)
                {
                    aName.add(eachKey);
                }
            }
           
           return aName;
           
       }
    Basically if the value of eachKey in the book map is greater or less than the arguments, it will add the name to the string set and output it right?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    if (bookMap.get(eachKey) >= startPage && <= endPage)
    This isn't valid Java. If a condition has two parts (eg joined by &&) then both parts have to be spelt out in full. if(foo>0 && foo<20).

    In English we might say "John is a man and very tall", in Java we must say "John is a man and John is very tall".
    SurfMan and jim829 like this.

  8. #8
    javacakes is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Quote Originally Posted by pbrockway2 View Post
    This isn't valid Java. If a condition has two parts (eg joined by &&) then both parts have to be spelt out in full. if(foo>0 && foo<20).

    In English we might say "John is a man and very tall", in Java we must say "John is a man and John is very tall".
    Thanks that makes sense, I got that part fixed.

    For the rest of it:

    I am having an extremely hard time figuring this out.

    Can someone please link me to some tutorials that would cover this as I can't seem to find any?
    I've been at this for almost over 48 hours now.
    Thanks

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Did you follow up on Toll's suggestion (entrySet)?

    Perhaps the Map section of Oracle's Tutorial will help. It's at The Map Interface (The Java™ Tutorials > Collections > Interfaces)

    There is an example there of using a for loop with keySet() to look at each of the keys. In the code you posted you loop over the map. Instead you should map over the map's key set. Then for each key, get the corresponding value from the map and check that. If it's within range you add the key (the word) to your set.
    javacakes likes this.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    Quote Originally Posted by pbrockway2 View Post
    There is an example there of using a for loop with keySet() to look at each of the keys. In the code you posted you loop over the map. Instead you should map over the map's key set. Then for each key, get the corresponding value from the map and check that. If it's within range you add the key (the word) to your set.
    That's what the entrySet is for, so you don't have to go back to the Map to retrieve the values.
    Those (often annoying) code checking tools will tend to flag up the use of a keySet to retrieve values and suggest using an entrySet instead.
    javacakes likes this.
    Please do not ask for code as refusal often offends.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to iterate through Map to find specific keys (based on the values) !?

    D@mn! You're quite right of course.

    Now you can see why code checking tools insist on being annoying: it's because of idiots like me.

Similar Threads

  1. Getting specific values from a file
    By M0RT in forum New To Java
    Replies: 2
    Last Post: 02-16-2012, 06:24 AM
  2. How do you find the OS specific linebreaker?
    By Addez in forum New To Java
    Replies: 1
    Last Post: 10-29-2009, 09:06 PM
  3. Replies: 0
    Last Post: 05-13-2009, 03:55 PM
  4. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  5. Uisng primitive type values as keys for Hashtable
    By ravian in forum New To Java
    Replies: 3
    Last Post: 11-21-2007, 10:13 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
  •