1

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());
    }
4
  • 4
    Map<Person, ArrayList<Location>> personByLocation = new HashMap<String, ArrayList<Location>>(); won't compile, I promise that. Commented Sep 3, 2015 at 15:34
  • OK. Whats is the right way to do it ? Commented Sep 3, 2015 at 15:36
  • Why use a Map in the first place? Use a Set<Person> and retrieve the locations from your Persons. Commented Sep 3, 2015 at 15:36
  • I`m just trying do write some code and in the same time to become betther with some data structures. Commented Sep 3, 2015 at 15:39

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes @andreas, great catch ;)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.