简体   繁体   中英

What if there is conflict of hashing of keys in ArrayMap or SparseArray?

I have read many blogs for array map and sparse array where every where I am getting that

ArrayMap contains two small array instead of one in a HashMap. The first array (Hash-Array) contains the specified hash keys in sorted order. The second array (Key Value Array) stores the keys and values of the objects according to the first array. For reference below link is given https://android.jlelse.eu/app-optimization-with-arraymap-sparsearray-in-android-c0b7de22541a

But no where I am getting that what happens if I have same hash code for two different keys in array map as in hash map it happens that in case having same key hash(collision) it appends the key value pair in linked list for a given bucket position for the next item.

Unlike the hashMap in ArrayMap when a hash collision occurs, instead of appending the key value pair in linked list for a given bucket position, for the next item it simply puts the value in next available position in second array. And for searching, when a hash collision occurs, it walk though the second array until finds the desired values .

Here it is described in provided doc:

When we fetch an item, a binary-search is done on the Hash-Array to find a matching hash the index and then directly return the key-value pair from the second array (Key Value Array). If the key in the second array (Key Value Array), doesn't match then a linearly walk is done over the second array (Key Value Array) to resolve the collision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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