简体   繁体   中英

How to return HashMap object fields

I currently have the following HashMap in a Holiday class.

Holiday Class:

HashMap <String, Location> holidays = new HashMap<String, Location>();

This creates an instance of the Location class, to allow more fields to be shown.

Location class:

public class Location {
private String locationName;
private String locationDesc;
private double price;
private int quantity;

public Location(String locationName, String locationDesc, double price) {
    this.locationName = locationName;
    this.locationDesc = locationDesc;
    this.price = price; 
    quantity = 0;
}

public String toString() {
    return (locationName + " | " + "£" + price);
}
public double getPrice() { return price; }
public String getLocationName() { return locationName; }
public String getLocationDesc() { return locationDesc; }
public int getQuantity() { return quantity; }
}

In my GUI class I just use the .get HashMap method, this will return the toString. eg

GUI class

private Holiday holiday;
...
return holiday.holidays.get(--HashMap key here--);

this will return the toString, which is locationName and price.

However. I'm wanting to also print out the HashMap elsewhere, but with returning different fields. such as returning the Description and quantity as well as locationName and price. How would i go about doing this? Or how do i return the individual fields from the Location class which is an instance of in the HashMap.

MANAGED TO DO THIS. But need help with the following below


Second EDIT:

I have a set quantity method in my Location class, to set the amount of bookings for each holiday. However when using;

for (Location location : holiday.holidays.values()) {
location.setQuantity(Integer.parseInt(textFieldQuantity.getText()));
}

This changes all of the holidays to the same quantity when setting each location with a different quantity. How do i fix this?

The result of holidays.get(key) should be an object of type Location . If you print the object directly, like in System.out.println(holidays.get(key)) it will print the result of toString() as you say. But since you already have the object and access to its fields, you can print exactly what you want.

Something like this should work:

Location location = holidays.get(key);
System.out.println(location.getlocationDesc() + " | " + location.getQuantity());

Regarding your second question:

If you just need to print all values stored in your map, I think it would be cleaner and faster to iterate directly on the map values:

for (Location location : holiday.holidays.values()) {
    System.out.println(location.getlocationDesc() + " | " + location.getQuantity());
}

Third question:

Note that your code does not set the quantity for only one location. It goes through all locations setting each quantity to the same value, defined by textFieldQuantity.getText() .

If you want to modify a specific location, you need to retrieve it from the map using get() :

Location location = holiday.holidays.get(key);
location.setQuantity(Integer.parseInt(textFieldQuantity.getText()));

Why not try something like:

private Location location = holiday.holidays.get(--HashMap key here--);

// Create a string with the variables

And then return the string.

this will return the toString, which is locationName and price

No. It will return an instance of Location. So all you have to do is

Location location = holiday.holidays.get("some key");
double price = location.getPrice();
String locationName = location.getLocationName();

Note that

  • you shouldn't use public fields. So it should be instead location.getHolidays().get("some key") . Or even better, to encapsulate the Map and respect the "don't talk to strangers" rule, location.getHoliday("some key") .
  • Your getter should be named getLocationName() and not getlocationName() to respect the JavaBean conventions. Or even better, since this method is part of a Location class, the location prefix is redundant, and you should thus simply name it getName() (and getDescription() for the description)

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