comparisons using a hashMap not giving expected result - java

The problem at hand is that when i try to pull the value from the hashmap it is returning null.
I have a hashMap instance :
Map<Marker,Tag> theHashMap = new HashMap<Marker,Tag>();
where tag is a class that holds some simple information about the marker and the marker is a Google map marker.
I add to theHashMap at the start of the activity that this all happens in
theHashMap.put(mapController.AddMarker(new Tag(1, "City Of Dundee", DUNDEE_LOCATION, "untagged",), new Tag(1, "City Of Dundee", DUNDEE_LOCATION, "untagged",));
where mapController is a class that deals with everything googleMap related.
And mapController.AddMarker returns the marker that was added to the map.
after the hashMap is filled it is passed to the onMarkerClickListener for later reference.
I call
hashMap.get(marker); from within a marker listener where marker is the marker that was clicked.
It always returns null, I thought it may be because the hashMap inside the onMarkerClick listener was a separate instance but I tried making a pointer to the original and it didn't work, i also tried hashMap.get(marker.getTitle()); and with the marker.getID() thinking that it would compare there titles but it ended with the same result.
I'll add more information if requested but for now, is there any other way of taking the value from the hashMap based on the marker that was clicked?

In your code hashMap.put(marker,key); here in this hashMap you have used key -> custom obje ct(mapper).
Hence it returns null when you called by the key. where provide key (mapper) object should match with put (mapper) key.
You have to override equals and hashcode methods in Mapper class to solve this.
EDIT:
In HashMap your key is mapper object(custom object). As custom object(Mapper object) is key in HashMap we have to override equals and hashcode in Mapper class to fetch exact value.
If key as Primitives(int) or String Object, no need to do the above thing.

Related

Map collection separate Object values

I have a Map collection that stores "stops" as Key and a set of grid and time results as an Object value. e.g.
Key: [stops]
Value: [[{grid_item=Grid1, time=09:30}, {grid_item=Grid13, time=10:00}, {grid_item=Grid3, time=10:15}, {grid_item=Grid10, time=10:35}]]
Is there a way to separate the Value results, because i would like to use the grid_item and time to send them to another method. How can i get those values specifically?
Or should i store again the values in a Map but now the keys are grid_item and time. But how can i do that?
Any Suggestions?
You can access a specific object in your value's array this way:
map.get(stops)[0] // grid_item=Grid1, time=09:30
map.get(stops)[1] // grid_item=Grid13, time=10:00
....
where stops is the key that you are using.

Passing custom HashMap between activities in Android

I have a HashMap which takes in a String ID(ID of a chatroom) as a key and stores all the chats from that specific room in an arraylist as follow:
HashMap<String, ArrayList<ChatMessage>> chatHistoryHashMap = new HashMap<String, ArrayList<ChatMessage>>();
This is created in RoomActivity and I want to pass it to ChatActivity. How am I able to do this? I tried to make it a public static but using the "put" method in ChatActivity seems to do nothing, not sure why.
I also need this HashMap to be passed back to RoomActivity when the back button is pressed in ChatActivity.
Do I need to use Intents?
When you made anything static in your class then that variable is visible to all classes in that package so it cannot happen that you are not able to get the value in another activity.
To access the static variable you can either make th object of the class or you can directly access it by using function name.
You can also pass the object using intent.putExtra() and intent.getExtra()

Java map value returns null

I have kind of a really strange problem. I have a simple Map called vectors where I store StrategyPairs as keys and Vectors as the values. When I print it, I get this result:
{net.softwarepage.facharbeit.normalgame.logic.StrategyPair#131e56d7=(1.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair#1e1bc985=(2.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair#d5415975=(0.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair#5bf8c6e7=(2.0;1.0)}
As you can see StrategyPair#131e56d7 is mapped to a Vector (1,2).
Now I create a new StrategyPair. When I print it I get StrategyPair#131e56d7 (the same one as before).
However, if I now call vectors.get(strategyPair) it returns null.
This is somehow really strange as the key is the same (at least it prints the exact same thing out when I print it...)
The problem arises when I rename a strategy, e.g. I change the property name in the class "Strategy". Then suddenly the map which contains StrategyPairs (a wrapper class for two strategies) is messed up as I explained before...
EDIT:
When I print the HashMap I still get the same result as above, but the following code:
for(StrategyPair pair : vectors.keySet()) {
System.out.println(vectors.get(pair));
}
returns:
null
(2.0;2.0)
null
(2.0;1.0)
As #Rajendra Gujja mentioned in a comment, the "hashcode of your keys should not change after you keep them in the map". This is very true; once I changed all the hashcodes to simply use a UUID instead of the name property which changes, the problem is solved. Thanks for all of your answers!

Quickest way to lookup a object

Suppose you have a list of Sunglasses objects where there are ...
4 attributes, color, shape, style and brand.
no identical ones; the combination of 4 attributes always different
What is the fastest way to retrieve them?
I think:
Override the hashcode() method in the Sunglasses class (should be unique because none of them are identical).
Then using each object's hashcode as key, object itself as value, put them into a Hashmap
Suppose I remember exactly what color shape and style and brand of a glass I want to
get,
I apply them with the hashcode method I have implemented.
then get it from the hashmap, this should give me contants time O(1) retrieval.
Problem is what if I only know the color. How do I get a list of all glasses with the same color?
Build a HashMap<Color,Collection<Glasses>> in addition to your other data structures.
This map essentially servers as an index on the Color attribute.
Whenever you add or remove glasses from your other data structures, make sure to update this color index as well.
Create a value class to hold the 4 attributes, and create hashcode and equals methods that use all the fields.
Use the class as a field of sunglasses instead of having separate fields (that way if you change it to add another field, it changes everywhere)
Use a hash map of value class --> sunglasses and when querying, build a value object at use map.get(value)

Is there a faster "Map"?

I have this GWT code below which have some slight problem:
It's either the login and aboutme property is set or just the contacts is set which ever comes first in the line. Could this be that the contacts property is set before the for-loop is finished, resulting in contacts being assigned a null?
public void copyFrom(User user) {
Map<String,String> map = new HashMap<String,String>();
for (Contact contact : user.getContacts()) {
map.put(contact.getType(), contact.getValue());
}
super.set("lastlogin", user.getLastLogin());
super.set("aboutme", user.getAboutMe());
super.set("contacts", map);
}
Do I need to use a "faster" Map?
Neither a for loop, nor adding values to a Map are asynchronous operations, so that entire loop executes prior to the calls to super.set(...). The map, in this case, cannot be null, since you instantiate it at the moment of declaration. It could end up being unpopulated (for instance, if there were no elements in user.getContacts()), but not null.
Your problem lies in the implementation of set(...) by whatever super is in this case.
If you are calling get on a Map for a key which does not exist null is returned, that's true. Your code is executed sequentially, so you can be sure that your for loop is finished when you access the map.

Categories

Resources