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

4991 questions
3964
votes
36 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
  • 47,479
  • 10
  • 30
  • 23
640
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
  • 68,948
  • 61
  • 139
  • 172
595
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
  • 42,353
  • 103
  • 307
  • 447
510
votes
16 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
  • 58,423
  • 9
  • 27
  • 25
294
votes
12 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
  • 2,941
  • 2
  • 14
  • 3
283
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
  • 14,602
  • 28
  • 88
  • 126
221
votes
5 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
  • 18,568
  • 14
  • 43
  • 56
181
votes
7 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
  • 2,237
  • 3
  • 17
  • 9
174
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
  • 19,065
  • 16
  • 62
  • 92
147
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,154
  • 9
  • 42
  • 46
135
votes
9 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
  • 1,883
  • 5
  • 18
  • 25
125
votes
17 answers

Associative arrays in Shell scripts

We required a script that simulates Associative arrays or Map like data structure for Shell Scripting, any body?
Irfan Zulfiqar
  • 1,739
  • 4
  • 14
  • 18
120
votes
8 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
  • 52,256
  • 15
  • 103
  • 110
117
votes
8 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,699
  • 7
  • 28
  • 30
107
votes
8 answers

Looping through a hash, or using an array in PowerShell

I'm using this (simplified) chunk of code to extract a set of tables from SQL Server with BCP. $OutputDirectory = "c:\junk\" $ServerOption = "-SServerName" $TargetDatabase = "Content.dbo." $ExtractTables = @( "Page" ,…
Sylvia
  • 2,406
  • 8
  • 29
  • 35
1
2 3
99 100