I am trying to fill in a Map which is declared like this
Map<Person, ArrayList<Location>> personByLocation =
new HashMap<String, ArrayList<Location>>();
These are Personand Location:
public class Person {
private Location location;
private String name;
public Person(Location location, String name) {
this.location = location;
this.name = name;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Location {
private LocationType locType;
private String locWeather;
public Location(LocationType locType, String locWeather) {
this.locType = locType;
this.locWeather = locWeather;
}
public LocationType getLocType() {
return locType;
}
public void setLocType(LocationType locType) {
this.locType = locType;
}
public String getLocWeather() {
return locWeather;
}
public void setLocWeather(String locWeather) {
this.locWeather = locWeather;
}
public enum LocationType {
Amsterdam, London, Wiena, Paris, Egypt;
}
}
I am trying to make a record in this Map but a do not now how to make it. If i make an instance of Person and put it as a Key the data which a record as value for this key will be duplicated with the data for Location in the ArrayList.
Here is what i did but it didn`t work at all.
Location location = new Location(LocationType.Paris, "sunny");
Person person = new Person(new Location(LocationType.Paris, "sunny"), "Timm");
for (Entry<Person, ArrayList<Location>> entry : personByLocation.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
Map<Person, ArrayList<Location>> personByLocation = new HashMap<String, ArrayList<Location>>();won't compile, I promise that.Mapin the first place? Use aSet<Person>and retrieve the locations from yourPersons.