简体   繁体   English

使用hashMap进行比较未给出预期结果

[英]comparisons using a hashMap not giving expected result

The problem at hand is that when i try to pull the value from the hashmap it is returning null. 当前的问题是,当我尝试从hashmap提取值时,它将返回null。

I have a hashMap instance : 我有一个hashMap实例:

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 . 其中tag是一个类,其中包含有关marker一些简单信息,而marker是Google地图marker

I add to theHashMap at the start of the activity that this all happens in 我在活动开始时将其添加到theHashMap中

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. 其中mapController是一个可处理与googleMap相关的所有内容的类。

And mapController . 还有mapController AddMarker returns the marker that was added to the map. AddMarker返回添加到地图的marker

after the hashMap is filled it is passed to the onMarkerClickListener for later reference. 填充hashMap后,将其传递给onMarkerClickListener以供以后参考。

I call hashMap.get(marker); 我叫hashMap.get(marker); from within a marker listener where marker is the marker that was clicked. marker listener ,其中marker是被单击的marker

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()); 它总是返回null,我认为可能是因为onMarkerClick侦听器内部的hashMap是一个单独的实例,但是我尝试制作一个指向原始实例的指针,但它不起作用,我还尝试了hashMap.get(marker.getTitle()); and with the marker.getID() thinking that it would compare there titles but it ended with the same result. 并带着marker.getID()认为可以比较标题,但结果相同。

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? 如果需要,我将添加更多信息,但是现在,是否还有其他方法可以基于单击的标记从hashMap中获取值?

In your code hashMap.put(marker,key); 在您的代码hashMap.put(marker,key); here in this hashMap you have used key -> custom obje ct(mapper). 在此hashMap中,您已经使用了key-> custom obje ct(mapper)。

Hence it returns null when you called by the key. 因此,当您通过按键调用时,它返回null。 where provide key (mapper) object should match with put (mapper) key. provide key (mapper)对象应with put (mapper) key.匹配的位置with put (mapper) key.

You have to override equals and hashcode methods in Mapper class to solve this. 您必须重写 Mapper类中的equals和hashcode方法来解决此问题。

EDIT: 编辑:

In HashMap your key is mapper object(custom object) . HashMap您的keymapper 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. 由于自定义对象(映射器对象)是HashMap中的关键,因此我们必须在Mapper class override equals和hashcode以获取确切值。

If key as Primitives(int) or String Object, no need to do the above thing.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM