Are the "key" and "value" terms for hash tables and associative arrays used interchangeably?

386 views Asked by At

I've been following this intro to CS course.

When I was learning C, I learned about hash tables.

Hash tables are arrays that require a hash function to map a "key" to an integer value. The value would be the index in the array.

"Key" -> [Hash function] -> value

array[value] = "Key"

Now, I'm learning about PHP, and I'm very confused about the use of associative arrays. In PHP we pass in a key (e.g. $_POST["key"] and it'll provide us with a value. So here the "key" is the index of the array, unlike the C hash table where they index was the value outputted by the hash function.

$_POST["key"] = Value

I've done a lot of searching and understand that hash tables and associative arrays are not 100% the same, but I'm very confused why these two different scenarios are using the terms "key" and "value" in different ways.

Am I seeing something wrong here?

3

There are 3 answers

1
T.J. Crowder On BEST ANSWER

"key" and "value" do not mean the same thing.

The key is the thing you feed into a hash table or PHP associative array or, generically, "map" to get back a value.

The confusion you're running into is that the value you get back from the hash table in your first example is then being used as a key (an array index) to a different thing (the array). Just as a person can be both a parent and a child, a number (or whatever) can be both a key (in one thing) and a value (in something else). It's a matter of what its role is in relation to the thing you're using it with.

0
Lee Daniel Crocker On

Generally speaking, a "key" is some piece of data used to uniquely identify and retrieve some "value" from a data structure of some kind. But of course the word "value" can mean many other things as well.

For example, if your language has built-in hash tables like Python or JavaScript, you can just say table[key] = value to store something and value = table[key] to retrieve it. If you're making your own hash tables, you might first have to compute a hash function of the key, and then compute an array index from the hash, and then put the value in the array at that index. A writer might refer to the result of a hash function as a "hash value", and he might refer to an array index as an array "key", so yes, it can get confusing.

0
Paul Ogilvie On

I am inclined to say that the word "key" is used wrong in the first exampe/statement of your CS course:

Hash tables are arrays that require a hash function to map a "key" to an integer value. The value would be the index in the array.

The statement could better be:

Hash tables are arrays that require a hash function to map an input value of some kind to an integer value, called the hash value. The hash value would be the index in the array.

input value -> [Hash function] -> hash value

Further, the statement:

array[value] = "Key"

is wrong because the hash function can genereate the same hash value for multiple input values. In general, array[value] will yield a list of all input values for which the hash function calculates the same hash value.

An associative array is an array notation to tell the machine to retrieve the information associated with a unique key (the array index) from a data set (the array name). Underneath the machine will execute an algorithm to search/retrieve this information. A programming language can provide the programmer with associative arrays (notation) to help the programmer write his program. So it is just an array notation, it is not array indexing, as in the hash array in C.