Does View
class overrides equals()
and hashcode()
methods? I want to create HashMap
with AdapterViews

- 4,683
- 18
- 29

- 847
- 1
- 7
- 10
-
1you do realize android is open source, right ? View code is here https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java – njzk2 Aug 08 '13 at 14:42
2 Answers
If I understand your question correctly, you need to implement HashMaps and are wondering if View
class or AdapterViews
will take care of it?
Well,
View
does not implement hashCode() and equals()
but they do import MAPS.
For the solution;
You will have to override the hashCode() and the equals() function in your implementation.
See implementing hash code for further help. Another Good source.
Hope this helps.
-
Thanks! That is great! I hoped they do not implement those methods. – Romans Stepanovs Aug 09 '13 at 13:57
It shouldn't matter if View
overrides those methods. Everything extends from Object
so there will be an implementation for them. Whether or not the implementation is specific to View
is (almost always) irrelevant. Therefore, you can use them as keys in a Map
.
Edit: While I couldn't find anything about View
and hashcode()
, I did find this wonderful page in the Android documentation that explains the base Java class, Object. In this documentation, it explains how to implement a hashcode()
method and what to do if an object doesn't support it (they recommend throwing an UnsupportedOperationException
). Therefore, I would have to assume that if a class in the Android library doesn't support hashcode()
, Google would do the same thing.

- 3,126
- 33
- 34
-
3I think the question is more : "is there a risk that 2 different views are considered equals and therefore unfit for hashmap key?" – njzk2 Aug 08 '13 at 14:42