Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are hashtables?

Tags:

hashtable

  • What are they and how do they work?
  • Where are they used?
  • When should I (not) use them?

I've heard the word over and over again, yet I don't know its exact meaning.

What I heard is that they allow associative arrays by sending the array key through a hash function that converts it into an int and then uses a regular array. Am I right with that?

(Notice: This is not my homework; I go too school but they teach us only the BASICs in informatics)

like image 533
keg Avatar asked Apr 12 '10 20:04

keg


People also ask

What is the purpose of Hashtables?

Hash tables let us implement things like phone books or dictionaries; in them, we store the association between a value (like a dictionary definition of the word "lamp") and its key (the word "lamp" itself). We can use hash tables to store, retrieve, and delete data uniquely based on their unique key.

What is a hash table in simple terms?

A hash table, also known as a hash map, is a data structure that maps keys to values. It is one part of a technique called hashing, the other of which is a hash function. A hash function is an algorithm that produces an index of where a value can be found or stored in the hash table.

Are hash tables really O 1?

Hash table is a great structure in terms of data management. The key-value scheme adopted by this data structure is intuitive and fits well with multiple data from different scenarios. Furthermore, the average complexity to search, insert, and delete data in a hash table is O(1) — a constant time.

What is an example of a hash table?

A hash table is simply an array that is addressed via a hash function. For example, in Figure 3-1, HashTable is an array with 8 elements. Each element is a pointer to a linked list of numeric data. The hash function for this example simply divides the data key by 8, and uses the remainder as an index into the table.


1 Answers

Wikipedia seems to have a pretty nice answer to what they are.

You should use them when you want to look up values by some index.

As for when you shouldn't use them... when you don't want to look up values by some index (for example, if all you want to ever do is iterate over them.)

like image 147
brabster Avatar answered Oct 06 '22 14:10

brabster