Professional Documents
Culture Documents
Before we get into the definition of Hash Tables, it is good to introduce WHY to use Hash tables.
For instance if we have an array full of data (say 100 items). If we knew the position that a
specific item is stored in an array, then we could quickly access it. For instance, we just happen
to know that the item we want it is at position 3; I can apply:
myitem=myarray[3];
With this, we don't have to search through each element in the array, we just access position 3.
The question is, how do we know that position 3 stores the data that we are interested in?
This is where hashing comes in handy. Given some key, we can apply a hash function to it to find
an index or position that we want to access.
Division Method
The most widely used hash function
The key k is divided by some number
D, and the remainder is used as the
bucket address.
h(k) = k % D
Since the bucket address is from 0 to
b-1 if there are b buckets, D is usually
selected as the number of buckets.
It will look like this
The important thing with the division method is that the keys are integers.
What happens when the keys aren't integers?
You have to apply another hash function to turn them into integers.
Effectively, you get two hash functions in one:
function to get an integer
function to apply a hash method from above to get an index to an array
What do we mean that the keys aren't integers? Well, let's say that the keys
are people's names. Such as:
Sarah Jones
Tony Balognie
Tom Katz
The goal is to type in one of these names and get an index to an
array in order to access that information. How do we do this?
The hash function has to do two things:
Convert the names into integers
For instance, we have a function which turns a string into an integer.
The results will be as follows:
Sarah Jones --> 1038
Tony Balognie --> 1259
Tom Katz --> 746
Folding
The key k is partitioned into several parts,
all of the same length. These partitions are
then added together to obtain the hash
address of k.
Two schemes
P1 Shift folding
P2 P3 P4 P5
Folding at the boundaries
1 2 3 2 0 3 2 4 1 1 1 2 2 0
Folding
P1 12 P1 12
3 3
P2 20 P2 30
P3 23
4 P3 22
4
P4 11
1 P4 21
1
P5 22
0 P5 21
0
69 89
Shift folding
9 Folding at the
7
boundaries
Mid-square
Squaring the key and then using an appropriate
number of bits from the middle of the square.
Example
Key = 113586,
Squaring the key, and then we have
1 2 9 0 1 7 7 9 3 9 6
h(x) =
1779
Again, the idea is that we will insert items into the hash table using the key
and applying the hash function(s) to get the index.
A problem occurs when two keys yield the same index. For Instance, say we
wanted to include:
John Smith --> 948 % 10 --> 8
We need a method to resolve this. The resolution comes in how you create
your hash table. There two major approaches given in the book:
1. Linear Probing
2. Rehashing
Linear Probing
Linear Probing - search sequentially until you find the
first vacant slot
Linear Probing is resolving a hash collision by sequentially
searching a hash table beginning at the location returned by
the hash function.
For example, if the hash function generates 79, then you
use array index 79 to store the element. When the hash
function generates the key 79 again, the program begins a
sequential search starting at location 79, looking for the next
available spot.
It will store at location 80,or 81 if these location occupied
It search farther until get the spot.
Rehashing
In this method we find an alternative empty
location by modifying the hash function.
For example if x is the key and h(x)=i. and i is
already occupied then we modify the hash
funtion h to h1 and find h1(x),
If h1(x) =j and j location is empty, then we
accommodate x in the j location otherwise again
modify the hash function till the collision get
handled.
HashTable put()/get() operations
Useful Operations:
put(Object key, Object value);
remove(Object key);
get(Object key);
The following example creates a hashtable of numbers.
It uses the names of the numbers as keys:
Hashtable numbers = new Hashtable(); numbers.put("one", new
Integer(1)); numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
To retrieve a number, use the following code:
Integer n = (Integer)numbers.get("two");
if (n != null) { System.out.println("two = " + n); }
Enumeration
Used to enumerate or iterate through a set
of values in a collection.
Useful for iterating Hashtables no index.
Useful Operations:
hasMoreElements();
nextElement();
import java.util.Hashtable;
import java.util.Enumeration;
/*
*/
/*
*/
if(hashtable.containsKey("One")){
System.out.println("Hashtable contains One as key");
}else{
System.out.println("Hashtable does not contain One as value");
}
/*
/*
IMPORTANT: get method returns an Object, so we need to downcast it.
Enumeration e = hashtable.keys();
while( e. hasMoreElements() ){
System.out.println(e.nextElement());
}
/*
e = hashtable.elements();
while(e. hasMoreElements()){
System.out.println(e.nextElement());
}
/*
To remove particular key - value pair from the Hashtable use remove method.
This method returns value that was mapped to the given key, otherwise null if mapping not found.
*/