我正在尝试填写一个像这样声明的地图
Map<Person, ArrayList<Location>> personByLocation =
new HashMap<String, ArrayList<Location>>();
这些是Person
和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;
}
}
我想在这张地图上做一个记录,但现在不知道怎么做。如果我创建Person的一个实例并将其作为一个Key放入一个记录作为该键值的数据将与ArrayList中的Location数据重复。
这是我做的,但它根本没用。
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());
}
答案 0 :(得分:2)
所有的拳头......这不会编译:
Map<Person, ArrayList<Location>> personByLocation = new HashMap<String, ArrayList<Location>>();
应将地图声明为:
Map<Person, ArrayList<Location>> personByLocation = new HashMap<Person, ArrayList<Location>>();
注意:我认为重点是,Person.location
是Location
的实际Person
,而List<Locations>
将是路线a人会做的。
创建位置和人员列表:
Location l1 = new Location(LocationType.Paris, "sunny");
Location l2 = new Location(LocationType.London, "cloudy");
Location l3 = new Location(LocationType.Wiena, "rain");
List<Location> list = new ArrayList<Location>();
list.add(l1);
list.add(l2);
list.add(l3);
Person person = new Person(new Location(LocationType.Paris, "sunny"), "Timm");
然后填写Map
:
personByLocation.put(person, list);
如果你添加了另一个Person
,那么它就不会被替换,而不是这个,如果你实际上在列表中的人,但更新了Person.location
和{{1它将被替换。
注意:还考虑替换List<Location>
实体的equals()
和hashCode()
方法。
答案 1 :(得分:0)
我不建议按照@Mena建议将一个人的所有位置放入班级Person
,只需从所有人那里取回Set<Location>
。这是因为seperation of concerns。
首先,您需要修复地图的实例化。
Map<Person, List<Location>> personByLocation = new HashMap<Person, List<Location>>();
其次,地图检索到的值是List
类型,为了打印每个元素,您实际上必须再次迭代它。
Location location = new Location(LocationType.Paris, "sunny");
Person person = new Person(location, "Timm");
for (Map.Entry<Person, List<Location>> entry : personByLocation.entrySet()) {
Person p = entry.getKey();
List<Location> locations = entry.getValue();
for(Location loc : locations) {
System.out.println(p + " " + loc);
}
}
向地图添加值时,首先必须检查该人员是否已存在。否则,您将覆盖已为该人员存储的值。
List<Location> myLocations = new ArrayList<Location>();
...
if(personByLocation.containsKey(person)) {
List<Location> storedLocations = personByLocation.get(person);
storedLocations.addAll(myLocations);
} else {
personByLocation.put(person, myLocations);
}