# Thread: help iterating over two maps

1. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## 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. ## Re: help iterating over two maps

Can you post the code that shows the definitions for the Maps?

3. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## 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. ## 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()) {

5. Moderator
Join Date
Apr 2009
Posts
13,319
Rep Power
23

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

6. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

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

8. Member
Join Date
Mar 2012
Posts
88
Rep Power
0

## 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. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

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

11. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## 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. ## Re: help iterating over two maps

Get the workers for the city.
Get the hours for each worker and look for a match.

13. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## 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. ## Re: help iterating over two maps

Just the workers for the selected city.

15. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

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

#### Posting Permissions

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