3

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.

Anand Kumar Jha
  • 614
  • 9
  • 23

1 Answers1

1

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.

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • Hi @ruhul thanks for reply. Could you please refer any doc where it could be found that the collision item is referred to next index of the second array. – Anand Kumar Jha Feb 04 '20 at 10:00
  • The text is quoted from the doc you've provided. https://android.jlelse.eu/app-optimization-with-arraymap-sparsearray-in-android-c0b7de22541a – MD Ruhul Amin Feb 04 '20 at 11:04
  • Yes it is for searching, but for insertion it is not clear. And if I am going through this link https://www.javatips.net/api/j2objc-master/jre_emul/android/frameworks/base/core/java/android/util/SparseArray.java here it is simply overwriting the previous object in put method. – Anand Kumar Jha Feb 05 '20 at 05:26