Just as an index is used for operations with respect to data structures such as Array or Vector, key is used for operations with regard to Maps.
The following table explains the API differences between these two data structures:
Operation | Array or Vector | Map |
Insertion |
add(index: Int) |
put(key: K, value: V) |
Deletion |
remove(index: Int): E |
remove(key: K): V |
Fetch |
get(index: Int): E |
get(key: K): V |
Updation |
set(index: Int) |
replace(key: K, value: V) |
As all operations of use a key, Map cannot follow a single rule for any operation, as keys can be of the Any type and can represent anything. For example, a key can be an Integer, a String, or a User. This will make all the operations perform slower, as Map can't address any entry directly. For example—in the case of Array or Vector, any operation is done using the direct...