Reputation: 803
I have 2 classes Gym and gymMember. Currently Gym stores all gymMember in a HashMap gymMembers where String is their member ID (String).
gymMember holds the variables:
this.fullName = aName;
this.address = anAddress;
this.area = anArea;
What i'd like to go is search through the the HashMap gymMembers to find out what members are in area 1 stores in variable this.area and then return the gymMember object to a new set.
So far i've got this far:
public void findCustomersInArea(int x)
{
// create a list to store all Customers
List<gymMember> aMember = new ArrayList<gymMember>();
// iterate all entries in the map
for (Map.Entry<String, gymMember> a: gymMembers.entrySet())
{
// get the customer
gymMember newMember = a.getArea();
if (newMember.getArea() == x) {
// do what you need to add to a list
newMember.add(newCustomer);
}
}
}
Does anyone have ideas where i'm going wrong?
Upvotes: 0
Views: 1328
Reputation: 631
While iterating through the Map
, instead of gymMember newMember = a.getArea();
you should be doing:
gymMember newMember = a.getValue();
Following would be your code:
for (Map.Entry<String, gymMember> a : gymMembers.entrySet()) {
// get the customer
gymMember newMember = a.getValue();
if (newMember.getArea() == x) {
// do what you need to add to a list
aMember.add(newCustomer);//The name of your list is aMember and not newMember
}
}
since you want to check the area
of each gymMember
and not area
of each entry
in the map
. In this case, since you are just accessing the values(i.e. gymMember
) associated with each id, you can do:
for (gymMember gymMember : gymMembers.values()) {//where your Object would be gymMember
if (gymMember.getArea() == x) {
aMember.add(gymMember);
}
}
Upvotes: 1
Reputation: 3154
gymMember newMember = a.getValue ();
if (newMember.getArea() == x) {
// do what you need to add to a list
aMember.add(newMember);
}
Upvotes: 1