Can hash functions work for more than one type of key?


#1

Question

In the context of this exercise, can hash functions work for more than one type of key?

Answer

Yes, you can implement a hash function to accept any type of key (string, integers, floats, booleans) and return some hash code.

However, mixing different types of keys in a hash map is usually not encouraged. One reason is that keys should try to share the same type, and stay consistent. You should usually try to implement a hash map so that it only takes in one type of key-value pair, which keeps things consistent and doesn’t cause any confusion. For instance, if we implement some hash map that lets us add any type of key-value pair, we wouldn’t be able to know what kind of information the hash map is storing.