Results 1 to 16 of 16
Thread: help iterating over two maps
- 04-30-2012, 12:15 PM #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.
ThanksLast edited by dojomat; 04-30-2012 at 02:20 PM.
- 04-30-2012, 01:05 PM #2
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.
- 04-30-2012, 01:13 PM #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
- 04-30-2012, 01:28 PM #4
Re: help iterating over two maps
Use the keySet method:how to iterate over the second map to get the workers
for(String key : map1.keySet()) {If you don't understand my response, don't ignore it, ask a question.
- 04-30-2012, 02:02 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-30-2012, 02:28 PM #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.
- 04-30-2012, 02:35 PM #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?If you don't understand my response, don't ignore it, ask a question.
- 04-30-2012, 02:43 PM #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
- 04-30-2012, 08:35 PM #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
- 04-30-2012, 08:44 PM #10
Re: help iterating over two maps
Is this how the data is organized:get the name of the workers that can work in an specified city and for the required hours
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.
- 04-30-2012, 08:53 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
- 04-30-2012, 08:59 PM #12
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.
- 04-30-2012, 09:26 PM #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
- 04-30-2012, 09:30 PM #14
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.
- 04-30-2012, 09:50 PM #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
- 04-30-2012, 10:05 PM #16
Similar Threads
-
Best way to make maps
By PhQ in forum New To JavaReplies: 8Last Post: 11-24-2011, 07:36 PM -
maps
By aizen92 in forum New To JavaReplies: 8Last Post: 05-24-2011, 09:53 AM -
Help with Maps
By snripa in forum New To JavaReplies: 4Last Post: 12-14-2010, 09:27 AM -
Maps and Sets
By RedKMan in forum New To JavaReplies: 3Last Post: 02-16-2010, 09:36 AM -
Maps
By natep67 in forum New To JavaReplies: 8Last Post: 05-06-2009, 03:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks