Questions tagged [hashtable]

A hash table in programming is a collection that uses a hash function to map identifying values (keys) to their associated values.

The basic advantage of a hash table is that they provide very efficient lookup and search functionality. Unlike many other linear collection types (such as arrays and linked lists), one does not have to loop through all the elements in a hash table searching for a particular entity.

For Java: https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html

5453 questions
4287
votes
35 answers

What are the differences between a HashMap and a Hashtable in Java?

What are the differences between a HashMap and a Hashtable in Java? Which is more efficient for non-threaded applications?
dmanxiii
  • 51,473
  • 10
  • 33
  • 23
814
votes
15 answers

How to define hash tables in Bash?

What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux).
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
607
votes
11 answers

How to do associative array/hashing in JavaScript

I need to store some statistics using JavaScript in a way like I'd do it in C#: Dictionary statistics; statistics["Foo"] = 10; statistics["Goo"] = statistics["Goo"] + 1; statistics.Add("Zoo", 1); Is there an Hashtable or something…
George2
  • 44,761
  • 110
  • 317
  • 455
540
votes
17 answers

How does a hash table work?

I'm looking for an explanation of how a hash table works - in plain English for a simpleton like me! For example, I know it takes the key, calculates the hash (I am looking for an explanation how) and then performs some kind of modulo to work out…
Arec Barrwin
  • 61,343
  • 9
  • 29
  • 25
318
votes
13 answers

What happens when a duplicate key is put into a HashMap?

If I pass the same key multiple times to HashMap’s put method, what happens to the original value? And what if even the value repeats? I didn’t find any documentation on this. Case 1: Overwritten values for a key Map mymap = new…
boodieye
  • 3,181
  • 2
  • 16
  • 3
295
votes
10 answers

.NET HashTable Vs Dictionary - Can the Dictionary be as fast?

I am trying to figure out when and why to use a Dictionary or a HashTable. I have done a bit of a search on here and have found people talking about the generic advantages of the Dictionary which I totally agree with, which leads the boxing and…
Jon
  • 15,110
  • 28
  • 92
  • 132
259
votes
4 answers

Is a Python dictionary an example of a hash table?

One of the basic data structures in Python is the dictionary, which allows one to record "keys" for looking up "values" of any type. Is this implemented internally as a hash table? If not, what is it?
Tommy Herbert
  • 20,407
  • 14
  • 52
  • 57
255
votes
8 answers

What's a correct and good way to implement __hash__()?

What's a correct and good way to implement __hash__()? I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries. As __hash__() returns an integer and is used for "binning" objects…
user229898
  • 3,057
  • 3
  • 19
  • 9
192
votes
14 answers

Good Hash Function for Strings

I'm trying to think up a good hash function for strings. And I was thinking it might be a good idea to sum up the unicode values for the first five characters in the string (assuming it has five, otherwise stop where it ends). Would that be a good…
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
172
votes
11 answers

hash function for string

I'm working on hash table in C language and I'm testing hash function for string. The first function I've tried is to add ascii code and use modulo (% 100) but i've got poor results with the first test of data: 40 collisions for 130 words. The final…
lilawood
  • 2,263
  • 5
  • 22
  • 27
164
votes
8 answers

How Do I Choose Between a Hash Table and a Trie (Prefix Tree)?

So if I have to choose between a hash table or a prefix tree what are the discriminating factors that would lead me to choose one over the other. From my own naive point of view it seems as though using a trie has some extra overhead since it isn't…
Justin Bozonier
  • 7,584
  • 9
  • 44
  • 46
152
votes
17 answers

Associative arrays in shell scripts

We require a script that simulates associative arrays or map-like data structure for shell scripting. Can anyone let's know how it is done?
Irfan Zulfiqar
  • 2,029
  • 5
  • 16
  • 21
137
votes
10 answers

Can hash tables really be O(1)?

It seems to be common knowledge that hash tables can achieve O(1), but that has never made sense to me. Can someone please explain it? Here are two situations that come to mind: A. The value is an int smaller than the size of the hash table.…
drawnonward
  • 53,459
  • 16
  • 107
  • 112
130
votes
8 answers

How to implement an efficient bidirectional hash table?

Python dict is a very useful data-structure: d = {'a': 1, 'b': 2} d['a'] # get 1 Sometimes you'd also like to index by values. d[1] # get 'a' Which is the most efficient way to implement this data-structure? Any official recommend way to do it?
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
128
votes
9 answers

Tuples (or arrays) as Dictionary keys in C#

I am trying to make a Dictionary lookup table in C#. I need to resolve a 3-tuple of values to one string. I tried using arrays as keys, but that did not work, and I don't know what else to do. At this point I am considering making a Dictionary of…
AlexH
  • 2,827
  • 7
  • 31
  • 30
1
2 3
99 100