Chapter 8 Hash Table (Part A)
Chapter 8 Hash Table (Part A)
Chapter 8
Page 1 of 34
What are Hash Tables
Key Value
001 “Simon So”
008 “Ng Sheung Tong”
003 “Ng Hup Gaat”
Operations:
Enter (put) a value with its associated key.
Look up (get) a value in the table using the key.
Page 2 of 34
What are Hash Tables
The ‘value’ can be any data type or objects.
Each key is unique (no two entries can have the same
key)
Different keys can have the same value
Key Value
“Apple” “A type of fruit”
“Zoo” “A place that with many animals to see”
“Java” “An island in Indonesia”
Page 3 of 34
What are Hash Tables
Key Value
SQUARE
TRIANGLE
CIRCLE
DIAMOND
Page 4 of 34
Hash Tables Operations
Put
Insert the value for a key into the table.
If an old value exists for the same key, the old value
will be replaced by the new value.
Key Value
Page 5 of 34
Hash Tables Operations
Get
Note: Users do not and need not know how the
value for a particular key is found in the table.
Key Value
key
Get
"S190012"
Page 6 of 34
The Java Hashtable class (java.util.Hashtable)
https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html
"A"
Page 7 of 34
The Java Hashtable class: String Example
import java.util.*;
...
Hashtable<String, String> myTable = new Hashtable<String, String>();
myTable.put(“China”, “Beijing”);
myTable.put(“UK”, “London”); key value
Page 8 of 34
myTable.put(“France”, “Paris”);
System.out.println(myTable.get(“China”)); //the output is “Beijing”);
System.out.println(myTable.get(“France”)); //the output is “Paris”);
System.out.println(myTable.get(“#$afr 33”)); //the output is null);
Page 9 of 34
Put and Get data into Hash Table in Java
import java.util.*;
- The class is defined in java.util
- (Alternatively, you can write java.util.Hashtable every time)
Page 10 of 34
Example
Key: Serial Number (integer number)
Value: Product Name (text)
Page 11 of 34
In the above examples, both the keys and values
are simple strings or integers.
Page 12 of 34
Solution: store them in an object before saving into hash ta-
ble!
Example:
Key: Bank Account Number (numbers with spaces and hyphens)
Value: BankAccount Object
Hashtable<String, BankAccount> data = new Hashtable<String, BankAccount> ();
Where BankAccount is defined as in previous chapters:
Page 13 of 34
Let’s create the BankAc-
count objects first.
BankAc- count a = new
BankAc- count(“015-
5144088888- 8”, “Lu”,
8000.12);
BankAccount b = new BankAccount(“015-5144099999-9”, “Mary”, 1000.23);
Then, use the accountNumber as key, and the BankAccount object as value:
data.put(“015-5144088888-8”, a);
data.put(b.accountNumber, b);
And get it back from the hash table using the correct accountNumber as the
Page 14 of 34
key:
BankAccount x = data.get(“015-5144088888-8”);
System.out.println(x.accountNumber + “ ” + x.customerName + “ ” + x.balance);
//Note: x will refer to the same object as a
Page 15 of 34
Remove from a Hash Table
Remove is similar to find, except that the entry will be deleted
after returning to you.
Hashtable<String, String> myTable = new Hashtable<String, String>();
myTable.put(“India”, “Bombay”);
System.out.println(myTable.get(“India”)); //output “Bombay”;
Page 16 of 34
Looping through all keys and values in a Hash Ta-
ble
Page 17 of 34
Looping through all keys and values
import java.util.*;
…
Hashtable<String, String> mytable = new Hashtable<String, String>();
mytable.put("A", "Apple");
mytable.put("B", "Boy");
mytable.put("C", "Cat");
Page 18 of 34
//Continuing from last page
System.out.println("The Hashtable contains:");
Page 19 of 34
Note:
Set<String> keys = myTable.keySet();
- Provides an unordered listing of all keys.
Page 20 of 34
Example: Create a table, and loop through all keys and values
Page 21 of 34
Time for a break!
Page 22 of 34
Page 23 of 34
How Does a Hash Table Work? (Part A)
Page 24 of 34
Looking up values in Hash Tables
Users do not and need not know how we search for the val-
ues.
Users don’t even know their position!
linear search.
But this is very inefficient!
"Principal"
"Principal"
Page 25 of 34
How do Hash Tables find the data quickly?
Value
"Principal"
Hash Function 17
17
Hash code
"Principal"
Hash Function 17
17
The hash Hash code
code is then used as
the index of the value in the table.
Page 28 of 34
Sometimes the hash function returns the Value
same
hash code for two different keys.
"Principal"
Hash Function 17
17
"Waiter"
Page 29 of 34
One solution is to link them up (like a linked list)!
table1.put("Principal", vp);
table1.put("Waiter", wp); Value
bucket
vp
"Principal"
17 17
wp
"Waiter"
Page 30 of 34
Hash Function: Example
(Note: this is already handled by the Hashtable in Java)
long hashcode;
hashcode = 0;
for (int i=0; i < s.length(); i++)
hashcode = hashcode*MULTIPLIER + (int)
s.charAt(i);
return (int) Math.abs((hashcode % nBuckets));
Page 31 of 34
}
Page 32 of 34
The previously shown hash function is typical of the func-
tions most often used in commercial applications.
The
mysterious
'w' 'a' 'i' 't' 'e' 'r' hashcode = 0; multiplier
hashcode = hashcode*MULTIPLIER + 119;
119 97 10 11 10 11 hashcode = hashcode*MULTIPLIER + 97;
5 6 1 4 hashcode = hashcode*MULTIPLIER + 105;
hashcode = hashcode*MULTIPLIER + 116;
hashcode = hashcode*MULTIPLIER + 101;
hashcode = hashcode*MULTIPLIER + 114;
Page 33 of 34
How to choose a Multiplier?
Page 34 of 34