简体   繁体   中英

How to store multiple objects from a hashmap that has the same key?

EDIT

I've tried this HashMap with multiple values under the same key , and my hashMap now looks like this HashMap<String, List<Place>> placeMap = new HashMap<>();

Also tried to put Object instead of Place(place is my superclass). But when I now create my subclasses and wants to add them to the HashMap I get:

The method put(String, List) in the type HashMap<String,List<Place>> is not applicable for the arguments (String, NamedPlace)

and

The method put(String, List) in the type HashMap<String,List<Place>> is not applicable for the arguments (String, DescPlace)

here is my adding which created the error:

NamedPlace p = new NamedPlace(x,y,answer,col,cat);
                placeMap.put(answer, p);

DescPlace dp = new DescPlace(x,y,answer, desc, col, cat);
                mp.add(dp);
                placeMap.put(answer, dp);

NamedPlace and DescPlace are both subclasses to Place, and I want them both in the same HashMap..

OP

I'm working on a little project here. The thing is that I need to use a HashMap instead of a ArrayList on this part of the project because HashMap is alot faster for searching. I've created a HashMap like this:

HashMap<String, Object> placeMap = new HashMap<>(); 

The String is the name of the Object, but the thing is that more than one object can have the same name. So I search for a object in my searchfield and I want to store all those objects that has that name into an ArrayList so I can change info in just them.

The object have alot of different values, like name, position, some booleans etc.

Do I need to create a HashCode method into my object class which shall create a unique hashcode?

When using a standard Map<String, List<YourClassHere>> instance, it is important to remember that the map's values for each entry will be a List<YourClassHere> , and will not handle it in any special way. So in your case, if you have

private Map<String, List<Place>> placeMap = new HashMap<>();

Then to store values you will need to do as follows:

NamedPlace p = new NamedPlace(x,y,answer,col,cat);
List<Place> list = placeMap.get (answer);
list.add(p);

However, this piece of code has some underlying problems.

  • It doesn't take into account that answer might not be present in placeMap .
  • It assumes that there's always a List<Place> instance for each key you query.

So the best way to fix those potential problems is to do as follows (Java 7 and later):

NamedPlace p = new NamedPlace(x,y,answer,col,cat);

if (placeMap.containsKey (answer) && placeMap.get (answer) != null) {
    placeMap.get (answer).add(p);
} else {
    List<Place> list = new ArrayList<Place> (); // ..or whatever List implementation you need
    list.add (p);
    placeMap.put (answer, list);
}

If you want to scna through the list of places, the code would look like this:

if (placeMap.containsKey (key) && placeMap.get (answer) != null) {
    for (Place p: placeMap.get (key)) {
        // Do stuff
    }
}

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