Reputation: 3031
I have HashMap<Shop, String> shopMap
where I put two values:
shopMap(shopModel1, shopModel1.getName());
shopMap(shopModel2, shopModel2.getName());
In my method for search shop by name I passed object of shop identical like shopModel1 to get his name:
public String getNameForShop(Shop filter) {
return shopMap.get(filter);
}
but I get null. Objects have the same all values. There is any way to get shop name from hash map using object?
Upvotes: 0
Views: 128
Reputation: 782
It is not ok to have the object as key value. Map it by name.
I guess mapping by object, in fact, it happens with the object's memory address. Then you search in map for an object it doesn't compare objects fields, just the addresses.
HashMap<String, Shop> shopMap;
if( shopMap.get("shopname").equals(anotherShop) ){
//do staff
}
Upvotes: 1