0

I created a post earlier today but it got marked as DUPLICATE and even after my edit I got no response on it. So I'll be trying to be more specific about just my problem..

info about my class

I've a class called Place which extends a class I call Position which is like the class Point, it has x and y coordinates. And now I create new classes which extends Place, called NamedPlace and DescPlace, it's not much that differ them, just that DescPlace have a description and NamedPlace doesn't..

My problem

I need to create a HashMap that where I can put my created objects into. I tried a hashMap like this HashMap<String, Place> placeMap = new HashMap<>(); This one works fine, but this is gonna be problem when I create a new NamedPlace called Pizza Resturant and later on I add another place called Pizza Resturant. Now I've two different objects with the same key in my HashMap

What i want to do

I've a searchfield where I enter the name, which is the key for my HashMap, and add ALL those object with that name to an ArrayList where I can't go through those objects one by one and change stuff. Like I've done here, but here I use just one arrayList to all, I need to have a HashMap that stores all objects and which I do the search in..

    class SearchLis implements ActionListener{
    public void actionPerformed(ActionEvent ave){
        for(int x = 0; x<placeList.size(); x++){
            if(placeList.get(x).getHighlighted()==true){
                placeList.get(x).setHighlighted(false);
            }
        }
        String search = searchField.getText();
        for(int i = 0; i<placeList.size(); i++){
            if(placeList.get(i).getName().equals(search)){
                placeList.get(i).setHighlighted(true);
                mp.repaint();
            }
        }
Filip Blom
  • 37
  • 1
  • 7
  • 3
    So, use a `HashMap>`. – Mena Apr 29 '15 at 12:31
  • Why not `Map>` ?? – Sercan Ozdemir Apr 29 '15 at 12:31
  • This is still a duplicate. If you have issues understanding the changes that the use of `HashMap>` involve, post that question. In fact, you should have posted the original question (as it is currently edited) instead of insisting in the original wording. – SJuan76 Apr 29 '15 at 12:33
  • Also, give some time to the moderation system to work to find if it reopens your original question after the edit. – SJuan76 Apr 29 '15 at 12:34
  • I get these errors with those `The method put(String, List) in the type Map> is not applicable for the arguments (String, NamedPlace)` @Mena @Sercan – Filip Blom Apr 29 '15 at 13:14
  • @FilipBlom that's likely because you are `put`ting a `String` key with value `? extends Place`, instead of `ArrayList`. Instead, check for `null` on `get`ting the key (if so, `put` an empty `ArrayList`), then `get` the key and `add` a `Place`. – Mena Apr 29 '15 at 13:17

0 Answers0