Results 1 to 16 of 16
  1. #1
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default help iterating over two maps

    Hi everyone,
    I've got lost trying to get that working.
    I have two maps. The first one contains cities as a key and people name's as a values.
    The second map contains people's name as a key and Integers as values (as the amount of hours they can work).
    IE:
    map1:
    String city {NYC, Sydney, Washington, London, Paris}.
    String name {{John, Emma}, {Phil, Karl}, ...}

    map2:
    String names {John, Emma, Phil, Karl, ...}
    Integers hours {{8,10}, {6,10}, {9,7}, {6,8}, ...}

    It takes two arguments, city and hours and I should return a list of people who is available in propose city and hours.
    Once I have the first iteration I don't know how to follow.
    I will appreciate any help or advice.
    Thanks


    for (String workers : map1.get(city))
    {
    possibleWorkers.add(workers);
    }


    I've iterated over the first map and get the possible workers but I don't know how to iterate over the second map to get the workers that can work an amount of hours.

    Thanks
    Last edited by dojomat; 04-30-2012 at 02:20 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    Can you post the code that shows the definitions for the Maps?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: help iterating over two maps

    The code is:

    private Map<String, ArrayList<String>> map1;
    private Map<String, ArrayList<Integer>> map2;

    Is that what you are asking for?

    Thanks in advance

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    how to iterate over the second map to get the workers
    Use the keySet method:
    for(String key : map1.keySet()) {
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: help iterating over two maps

    You don't need to iterate over the second Map.
    You need to iterate over the List<String> returned from your get(<cityname>), which you;re already doing.
    That gives you the key to do a get(worker) on the second map, which will give you the List<Integer>.
    You only need that one loop that you already have.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: help iterating over two maps

    Thanks Norm,

    Once I iterated:

    for (String workers : map1.get(city))
    {
    possibleWorkers.add(workers);
    }

    I can get the workers by city (which is the first argument of the method). How do I need to iterate over the second map to know that workers chosen by city (as iterated before) and using the second argument (the amount of hours they can work) and display only this workers?
    ie: the first argument is NYC (city) and the second argument is 10 (hours to work)

    Thanks again and sorry if I look thick, just building my knowledge on java slowly.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    Can you explain what data is in each Map and what you need to get from each Map?
    There are workers, cities and hours. What is the objective?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: help iterating over two maps

    Hi mate,

    You will need to create a new collection that holds these hours and then use a condition to check if your method argument(hours) is in this new collection.

    Hope this gets you further.

    Regards

  9. #9
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: help iterating over two maps

    Hi Norm,
    the data in each map are:

    private Map<String, ArrayList<String>> map1;
    private Map<String, ArrayList<Integer>> map2;


    map1:
    String city {NYC, Sydney, Washington, London, Paris}.
    String name {{John, Emma}, {Phil, Karl}, ...}

    map2:
    String names {John, Emma, Phil, Karl, ...}
    Integers hours {{8,10}, {6,10}, {9,7}, {6,8}, ...}

    The objective is to get the name of the workers that can work in an specified city and for the required hours (as the arguments).
    Thanks

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    get the name of the workers that can work in an specified city and for the required hours
    Is this how the data is organized:
    Each city has a list of workers.
    Each worker has a range(???) of hours
    What do the hours values represent? Must their values match exactly or how do you decide if the hours fit?
    Does the data say that John can work 8 or 10 hours?

    The task:
    Given a range(???) of hours and a city, find the workers.
    Last edited by Norm; 04-30-2012 at 08:48 PM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: help iterating over two maps

    The values of the hours must match the hours asked for (ie John could work or 8 or 10 hours).

    Everything else is correct: If the arguments were NYC and 10, the result should be John and Emma as the are linked to that city and can work 10 hours.

    Thanks again

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    Get the workers for the city.
    Get the hours for each worker and look for a match.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: help iterating over two maps

    I can get the workers for the city.
    get the hours for each worker of the selected city?? or all the workers?

    Thanks

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    Just the workers for the selected city.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    dojomat is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: help iterating over two maps

    So you mean:

    for (String workers : map1.get(city))
    {
    possibleWorkers.add(workers); // now we know the workers from the argument (city)
    for (List<Integer> workingHours: map2.get(possibleWorkers))
    {
    possibleHours.add(workingHours); // now we should know the sets of hours of the selected workers
    }
    }

    Is that correct? If not, could you write some code to help me understand, please?

    Thanks

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: help iterating over two maps

    Something like:
    Java Code:
    workers = mapCityToWorkers.get(nameOfCity);
    for (Worker aWorker : workers)
      hours = mapWorkersToHours(aWorker);
    // now scan hours for the requested hours
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Best way to make maps
    By PhQ in forum New To Java
    Replies: 8
    Last Post: 11-24-2011, 07:36 PM
  2. maps
    By aizen92 in forum New To Java
    Replies: 8
    Last Post: 05-24-2011, 09:53 AM
  3. Help with Maps
    By snripa in forum New To Java
    Replies: 4
    Last Post: 12-14-2010, 09:27 AM
  4. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 09:36 AM
  5. Maps
    By natep67 in forum New To Java
    Replies: 8
    Last Post: 05-06-2009, 03:59 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
  •