You are on page 1of 23

Hash Table

1. Definition of a Hash Table

HashTable is the class which provides hash tables in Java.

Hash table are supported by Java in the form of java.util.Hashtable class.


Hash table accepts as keys and values any java objects.

Before we get into the definition of Hash Tables, it is good to introduce WHY to use Hash tables.

Hash tables are good for doing a quick search on things.

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.

Bucket in Hash table


Division
Mid-square
Folding
Digit Analysis
1.2 Division method (one hash method for integers)
Let's say you had the following numbers or keys that you wanted to
map into an array of 10 elements:
123456
123467
123450
To apply the division method, you could divide the number by 10 (or
the maximum number of elements in the array) and use the
remainder (the modulo) as an index. The following would result:

123456 % 10 = 6 (the remainder is 6 when dividing by 10)


123467 % 10 = 7 (the remainder is 7)
123450 % 10 = 0 (the remainder is 0)
These numbers would be inserted into the array at positions 6, 7,
and 0 respectively. It might look something like this:

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

Apply a hash method to get an index


We can now apply the division method to get an index for an array
of 10 elements
Sarah Jones --> 1038 % 10 --> 8
Tony Balognie --> 1259 % 10 --> 9
Tom Katz --> 746 % 10 --> 6
What would that look like in the array?
The array is known as a hash table. It stores the key (used to find the index) along
with associated values. In the above example, we might have a hash table that

looked something like this:

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 have a collision because Sarah Jones is already stored at array index


8.
999 % 100 = 99 (100 is Table size)
524 % 100 = 24
199 % 100 = 99 (COLLISION)

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;

public class HashtableExample{

public static void main(String args[]){

// constructs a new empty hashtable with default initial capacity


Hashtable hashtable = new Hashtable();

/* To specify initial capacity, use following constructor


Hashtable hashtable = new Hashtable(100);*/
hashtable.put("One", new Integer(1)); //adding value to Hashtable

hashtable.put("Two", new Integer(2));

hashtable.put("Three", new Integer(3));


get number of keys present in the hashtable
System.out.println("Hashtable contains " + hashtable.size() + " key value pairs.");

/*

To check whether Hashtable is empty or not, use isEmpty() method.


isEmpty() returns true is Hashtable is empty, otherwise false.
Signature of the contains method is,
boolean contains(Object value)

*/

if( hashtable.contains(new Integer(1))){


System.out.println("Hashtable contains 1 as value");
}else{
System.out.println("Hashtable does not contain 1 as value");
}

/*

Finding particular Key from the Hashtable :

Hashtable's containsKey method returns boolean


depending upon the presence of the key in given hashtable

Signature of the method is,


boolean containsKey(Object key)

*/

if(hashtable.containsKey("One")){
System.out.println("Hashtable contains One as key");
}else{
System.out.println("Hashtable does not contain One as value");
}

/*

Use get method of Hashtable to get value mapped to particular key.

Signature of the get method is,


Object get(Object key)
*/

Integer one = (Integer) hashtable.get("One");

System.out.println("Value mapped with key \"One\" is " + one);

/*
IMPORTANT: get method returns an Object, so we need to downcast it.

System.out.println("Retrieving all keys from the Hashtable");

Enumeration e = hashtable.keys();

while( e. hasMoreElements() ){
System.out.println(e.nextElement());
}

/*

To get all values stored in Hashtable use elements method.

Signature of the elements method is,


Enumeration elements()*/
System.out.println("Retrieving all values from the Hashtable");

e = hashtable.elements();

while(e. hasMoreElements()){
System.out.println(e.nextElement());
}

/*

To remove particular key - value pair from the Hashtable use remove method.

Signature of remove method is,


Object remove(Object key)

This method returns value that was mapped to the given key, otherwise null if mapping not found.

*/

System.out.println( hashtable.remove("One") + " is removed from the Hashtable.");


Output would be :

Hashtable contains 3 key value pair.


Hashtable contains 1 as value
Hashtable contains One as key
Value mapped with key "One" is 1
Retrieving all keys from the Hashtable
One
Three
Two
Retrieving all values from the Hashtable
1
3
2
1 is removed from the Hashtable

You might also like