2

I read that HashTable can map same key to multiple values. That's what collision is.

Now I run the program like this:

Dictionary<String,String> hTable = new Hashtable<String,String>();
hTable.put("a", "aa");
hTable.put("a", "ab");
System.out.println(""+hTable.get("a"));

My thinking says I should get aa and ab.

But actual output is ab

Why is it so? Where is the collision then?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
user900721
  • 1,417
  • 4
  • 17
  • 29
  • Collisions are an internal implementation detail, and have to do with when two keys hash the same way, not when the same key is used multiple times. As @Mehrdad points out, they are resolved for you transparently (modulo performance degradation). If they weren't, then the behavior you'd see wouldn't be multiple values for a single key, but rather some keys getting mysteriously overwritten by other keys (that happened to hash to the same table cell). – Laurence Gonsalves Aug 20 '11 at 04:43
  • @Lauenece: I did not get 1) have to do with when two keys hash the same way 2)the behavior you'd see wouldn't be multiple values for a single key, but rather some keys getting mysteriously overwritten by other keys. Please explain a bit more on it. – user900721 Aug 20 '11 at 07:11
  • I can't really explain it any more clearly in the space of a comment. Try reading about hash tables in a good algorithms book or maybe the Wikipedia article, http://en.wikipedia.org/wiki/Hash_table . Keep in mind that these are implementation details. If you just want to *use* the `Hashtable` class you don't really need to know these details - just understand the interface/contract. `Hashtable` extends `Dictionary`, so it adheres to `Dictionary`'s contract, and `Dictionary`'s docs say "In any one `Dictionary` object, every key is associated with at most one value". – Laurence Gonsalves Aug 20 '11 at 20:48

5 Answers5

3

There is no collision. A HashTable entry maps a key to only one value.

The third line in your sample:

hTable.put("a", "ab");

replaces the mapping from a to aa with a mapping from a to ab.

After your four lines of code complete execution, hTable has only one mapping: a to ab.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
3

A collision only happens internally. To the user, are resolved transparently.

That's why a hashtable can be a dictionary -- it maps each key to exactly 1 value. If it mapped to more than 1 value then it wouldn't be a dictionary.

user541686
  • 205,094
  • 128
  • 528
  • 886
2

Hashtable doesn't map the same key to multiple values. Collision is that multiple keys might be mapped to the same hash value. It is resolved by the data structure itself which is transparent to you.

If you want to get aa and ab by hTable.get("a"), you need to create Dictionary<String,List<String>> and append the list with the values of the same key.

In your code

hTable.put("a", "aa");
hTable.put("a", "ab");

The keys are the same. So the second operation used "ab" to override "aa". That's why you only get "ab".

Mu Qiao
  • 6,941
  • 1
  • 31
  • 34
  • ok, what it means: "multiple keys might be mapped to the same hash value" I thought if key is same then hash value is also same. Am I right? – user900721 Aug 20 '11 at 08:15
  • Yes you are right. But even if the keys are not the same, the hash value can be the same. And that is so called "collision". – Mu Qiao Aug 21 '11 at 15:24
  • Ok Perfect. I understood. Thanks ! Next thing then In case of collision how it knows which value has to retrieve then? – user900721 Aug 22 '11 at 03:51
  • The latter value will override the the privious one. There is always one value corresponding to the key all the time. – Mu Qiao Aug 22 '11 at 04:27
2

HashTable is Key -> Value mapping. That means you can not have multiple values for more one key. You need to combine two data structures store multiple values with one key.

For Example, You can put a linkList inside you HashTable. For example

HashTable<String,LinkedList<String>> table = new HashTable();
LinkedList<String> list = new LinkedList();
list.add("aa");
list.add("ab");
table.add("a",list);

now you can do this get aa and ab value;

table.get("a").get(0); // returns aa
table.get("a").get(1); // returns ab

I strongly recommend you to go through the basics of data structure and algorithm.

arpanoid
  • 2,123
  • 4
  • 17
  • 15
1

You want to retrieve values by their keys. An array serves this purpose but is restricted to using integer keys and may use too much space (think about storing values at position 0 and 1000 only, you have to allocate the entire array for 2 elements).

HashTables solve both of these problems with:

  • a dispersive non-injective function that converts an array of bytes of variable length in a fixed length array of bytes. This means that you have hash(bytes_1) == hash(bytes_2) but it doesn't happen too often and if bytes_1 ~ bytes_2 the hashes are different;
  • an index of used hashes. If the function returns an array of 10 bytes you have 2^80 possibilities, so you need to keep a sorted list of the hashes that you already encountered;
  • an array of linked lists. The index of hashes maps the hash with the position in the array.

The collision means that two keys have the same hash: map.put("key1", "value1"); map.put("key2", "value2") key1 and key2 might wind up in the same linked list.

mradu
  • 54
  • 4