0

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?

Community
  • 1
  • 1
Filip Blom
  • 37
  • 1
  • 7
  • Use a `Map>` – JB Nizet Apr 29 '15 at 10:55
  • Since the map value is now a `List`, you must `set` and `get` objects of that type to/from the map. To add a `NamedPlace`, retrieve the list (if it exists) and **add the `NamedPlace` to the list** – SJuan76 Apr 29 '15 at 12:39
  • You came very close to the solution, just change `placeMap.put(answer, p);` to `placeMap.get(answer).add(p);`. Make sure, that you don't run into uninitialized lists for each entry of the map. – SME_Dev Apr 29 '15 at 12:40
  • **Multimap** try that – Srinath Ganesh Apr 29 '15 at 14:05

1 Answers1

0

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
    }
}
Laf
  • 7,965
  • 4
  • 37
  • 52
  • Why do you write "assuming you are using Java 8" when you don't make use of it's features (Stream API surely is useful for the task at hand)? Instead, this is plain old java way since generics were introduced in 2004. – SME_Dev Apr 29 '15 at 13:17
  • @SME_Dev Because I used `new ArrayList<> ()`. True, I could have used the Stream API, or should have, but I manually tryped the code without trying it in a compiler first, and since I don't fully master the Stream API, I chose not to include it. And to make it simpler too for the OP, I assumed he does not has a vast experience in Java. – Laf Apr 29 '15 at 13:21
  • Hey, thanks for the feedback Laf, the only problem I get with this is the row `placeMap.get(answer).list.add(p);` i get `list cannot be resolved or is not a field` error message.. The list in else works fine. I even tried to move `List list = new ArrayList ();` outside every method – Filip Blom Apr 29 '15 at 13:41
  • 1
    @Laf `new ArrayList<> ()` the empty diamond operator was introduced with Java 7. @Filip Blom `(answer).list.add(p)` was probably just a typo. Either declare the `list` before the `if` statement, or don't declare the variable at all by writing `placeMap.get(answer).add(p)`. – SME_Dev Apr 29 '15 at 13:45
  • @FilipBlom YEah I made a typo. I've just fixed it now. SME_Dev: true, it was indeed introduced in Java 7. But the code example is still valid in Java 8 ;) – Laf Apr 29 '15 at 13:52
  • Thanks to both of you! I've been trying with these little code for way to long. You just made my day :) – Filip Blom Apr 29 '15 at 14:00