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
78
votes
7 answers

Hash table - why is it faster than arrays?

In cases where I have a key for each element and I don't know the index of the element into an array, hashtables perform better than arrays (O(1) vs O(n)). Why is that? I mean: I have a key, I hash it.. I have the hash.. shouldn't the algorithm…
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
78
votes
2 answers

How do I encode a JavaScript object as JSON?

Is there a good way to encode a JavaScript object as JSON? I have a list of key value pairs...where the name is from a checkbox, and the value is either true or false based on whether the box is checked or not: var values = {}; $('#checks…
daniel langer
  • 1,855
  • 2
  • 17
  • 20
73
votes
14 answers

Looking for a good hash table implementation in C

I am primarily interested in string keys. Can someone point me towards a library?
Setjmp
  • 27,279
  • 27
  • 74
  • 92
72
votes
7 answers

iterating through Enumeration of hastable keys throws NoSuchElementException error

I am trying to iterate through a list of keys from a hash table using enumeration however I keep getting a NoSuchElementException at the last key in list? Hashtable vars = new Hashtable(); vars.put("POSTCODE","TU1…
David Cunningham
  • 957
  • 2
  • 12
  • 22
72
votes
15 answers

How do you implement GetHashCode for structure with two string, when both strings are interchangeable

I have a structure in C#: public struct UserInfo { public string str1 { get; set; } public string str2 { get; set; } } The only rule is that UserInfo(str1="AA", str2="BB").Equals(UserInfo(str1="BB",…
Graviton
  • 81,782
  • 146
  • 424
  • 602
72
votes
10 answers

Binary Trees vs. Linked Lists vs. Hash Tables

I'm building a symbol table for a project I'm working on. I was wondering what peoples opinions are on the advantages and disadvantages of the various methods available for storing and creating a symbol table. I've done a fair bit of searching and…
benmcredmond
  • 1,702
  • 2
  • 15
  • 22
72
votes
1 answer

Improving performance of very large dictionary in Python

I find that if I initialize an empty dictionary at the beginning, and then adding elements to the dictionary in a for loop (about 110,000 keys, the value for each key is a list, also increasing in the loop), the speed goes down as for loop goes. I…
szli
  • 36,893
  • 11
  • 32
  • 40
70
votes
8 answers

How do I create a Dictionary that holds different types in C#

I need some sort of way to store key/value pairs where the value can be of different types. So I like to do: int i = 12; string s = "test"; double x = 24.1; Storage.Add("age", i); Storage.Add("name", s); Storage.Add("bmi", x); And later…
Enrico
  • 1,937
  • 3
  • 27
  • 40
70
votes
7 answers

What is the true difference between a dictionary and a hash table?

I've always used dictionaries. I write in Python.
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
70
votes
3 answers

Why there is a Thread.Sleep(1) in .NET internal Hashtable?

Recently I was reading implementation of .NET Hashtable and encountered piece of code that I don't understand. Part of the code is: int num3 = 0; int num4; do { num4 = this.version; bucket = bucketArray[index]; if (++num3 % 8 == 0) …
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
69
votes
11 answers

Why can't you use null as a key for a Dictionary?

Apparently, you cannot use a null for a key, even if your key is a nullable type. This code: var nullableBoolLabels = new System.Collections.Generic.Dictionary { { true, "Yes" }, { false, "No" }, { null, "(n/a)"…
devuxer
  • 41,681
  • 47
  • 180
  • 292
67
votes
6 answers

What hashing function does Java use to implement Hashtable class?

From the book CLRS ("Introduction to Algorithms"), there are several hashing functions, such as mod, multiply, etc. What hashing function does Java use to map the keys to slots? I have seen there is a question here Hashing function used in Java…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
63
votes
5 answers

What is a hash map in programming and where can it be used

I have often heard people talking about hashing and hash maps and hash tables. I wanted to know what they are and where you can best use them for.
Saif Bechan
  • 16,551
  • 23
  • 83
  • 125
63
votes
5 answers

Chained Hash Tables vs. Open-Addressed Hash Tables

Can somebody explain the main differences between (advantages / disadvantages) the two implementations? For a library, what implementation is recommended?
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
62
votes
7 answers

How does Java order items in a HashMap or a HashTable?

I was wondering how Java orders items in the Map (HashMap or Hashtable) when they are added. Are the keys ordered by the hashcode, memory reference or by allocation precedence...? It's because I've noticed same pairs in the Map are not always in the…
Eyad Salah
  • 1,084
  • 1
  • 11
  • 22