简体   繁体   中英

Access an object attribute from the Hashmap

I'm currently creating a simple taxi dispatch system for a java assignment.

the Taxi class contains the attribute platenumber.

I must have to create a hashmap that takes an area as a key and an ArrayList of taxi as the value. There are 6 different areas, and 50 unique Taxis(platenumber is what make them unique). each different area(key) is supposed to have a unique set of plates(or Taxis). and all areas are not to have more than 50 plates combined.

Now for my question,

Is there a way that I can access the object attribute (taxi plate number) within the the arraylist of taxi objects that is in the areas hashmap.

so,

areas.values()

will return my arraylist of Taxis

however I would like to get the plate number of the taxi objects that is in the taxi array list which is in the areas hashmap.

is this possible?

Is there a way that I can access the object attribute (taxi plate number) within the the arraylist of taxi objects that is in the areas hashmap.

Sure. To print the plate numbers for all taxis in a certain area, you would do:

List<Taxi> taxiesInArea = yourHashMap.get(area);

for (Taxi taxi : taxiesInArea)
    System.out.println(taxi.plateNumber());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM