简体   繁体   中英

How to make a java map with this parameters Map<Object, ArrayList<Object>>?

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 Person and 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());
    }

Fist of all... this wont compile:

Map<Person, ArrayList<Location>> personByLocation = new HashMap<String, ArrayList<Location>>();

Should declare map as:

Map<Person, ArrayList<Location>> personByLocation = new HashMap<Person, ArrayList<Location>>();

NOTE: I think the point is, Person.location is the actual Location of the Person and the List<Locations> will be the route a person will do.

Create a list of locations and a person:

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");

Then fill the Map :

personByLocation.put(person, list);

If you add another Person as is the key it won't be replaced, instead of this, if you take a person actually in the list but with updated Person.location and List<Location> it will be replaced.

NOTE: also think about replacing equals() and hashCode() method of the Person entity.

I would not suggest to follow @Mena suggestion to put all the locations of a person into the class Person and just retrieve a Set<Location> from all your persons. This is, because of seperation of concerns .

First of all you need to fix your instantiation of the map.

Map<Person, List<Location>> personByLocation = new HashMap<Person, List<Location>>();

Second the retrieved values by the map are of type List which you actually have to iterate over again in order to print every element.

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);
    }
}

When you add a value to the map you first have to check if for the person an entry already exists. Otherwise you would override the values already stored for that person.

List<Location> myLocations = new ArrayList<Location>();
...
if(personByLocation.containsKey(person)) {
    List<Location> storedLocations = personByLocation.get(person);
    storedLocations.addAll(myLocations);
} else {
    personByLocation.put(person, myLocations);
}

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