Class Notes: Implementing Hashmaps (Implementation View) Today Is Also Pencil/paper
Class Notes: Implementing Hashmaps (Implementation View) Today Is Also Pencil/paper
(Implementation View)
import java.util.HashMap;
Today’s goal: discuss how to implement a HashMap class for ourselves (finish for hwk!)
Conceptual Hat
Work this example – (note array is now size 4)
• Run the code by hand: what will print out?
• Fill in the diagram on the right with
• the arrows from keys to indices, and
• the final content of the array cells
import java.util.HashMap; %
mod
HashMap<Integer,String> IDtoName = new HashMap<...>
IDtoName.put(100003, “A”); 100003 0
IDtoName.put(100016, “B”); 100016 1
IDtoName.put(100007, “C”);
IDtoName.put(100021, “D”); 100007 2
IDtoName.put(100016, ”E”); 100021 3
System.out.println(IDtoName.get(100016));
System.out.println(IDtoName.get(100003));
System.out.println(IDtoName.get(100007));
import java.util.HashMap; %
mod
HashMap<Integer,String> IDtoName = new HashMap<...>
IDtoName.put(100003, “A”); 100003 0
X ”E”
”B”
IDtoName.put(100016, “B”); 100016 1
IDtoName.put(100007, “C”); ”D”
IDtoName.put(100021, “D”); 100007 2
IDtoName.put(100016, ”E”); 100021 3 ”A” ”C”
System.out.println(IDtoName.get(100016));
System.out.println(IDtoName.get(100003));
System.out.println(IDtoName.get(100007));
Conceptual Hat
Work this example – (note array is now size 4)
• Run the code by hand: what will print out?
• Fill in the diagram on the right with
• the arrows from keys to indices, and
• the final content of the array cells
import java.util.HashMap; %
mod
HashMap<Integer,String> IDtoName = new HashMap<...>
100003 0
IDtoName.put(100003, “A”); X ”E”
”B”
IDtoName.put(100016, “B”); 100016 1
IDtoName.put(100007, “C”); ”D”
IDtoName.put(100021, “D”); 100007 2
IDtoName.put(100016, ”E”); 100021 3 ”A” ”C” collision!
System.out.println(IDtoName.get(100016));
System.out.println(IDtoName.get(100003));
System.out.println(IDtoName.get(100007)); Under the hood, must allow
multiple values
(from different keys that map
to same index)
Conceptual Hat
Work this example – (note array is now size 4)
class KVPair<K,V> {
• Run the code by hand: what will print out? public K key;
• Fill in the diagram on the right with public V value;
}
• the arrows from keys to indices, and
• the final content of the array cells class HashTable<K,V> {
int size; // number of slots
LinkedList<KVPair<K,V>> contents;
import java.util.HashMap; } %
mod
HashMap<Integer,String> IDtoName = new HashMap<...>
100003 0
IDtoName.put(100003, “A”); X ”E”
”B”
IDtoName.put(100016, “B”); 100016 1
IDtoName.put(100007, “C”); ”D”
IDtoName.put(100021, “D”); 100007 2
IDtoName.put(100016, ”E”); 100021 3 ”A” ”C” collision!
System.out.println(IDtoName.get(100016));
System.out.println(IDtoName.get(100003));
System.out.println(IDtoName.get(100007)); KVPair(100003,”A”)
LinkedList<KVPair>
KVPair(100007,”C”)
Key Takeaways
• From a programmer’s perspective, there can only be one value per key
• Under the hood, may have multiple keys that map to the same array slot (after
generating the hash code and compressing via modulo). THIS IS OKAY!
• Under the hood, a hashtable maintains a list within each array slot of all of the
key-value pairs that map to that bucket of the hashtable
Homework Preview – implement hashtables
interface IDictionary<K,V> { What are the running
// Looks up a value in the dictionary, given its key. times of all of these?
// throws KeyNotFoundException if the key is not found
public V lookup(K key) throws KeyNotFoundException;
interface IDictionary<K,V> {
public V lookup(K key) throws KeyNotFoundException;
public V update(K key, V value) throws KeyNotFoundException;
public void insert(K key, V value) throws KeyAlreadyExistsException;
public V delete(K key) throws KeyNotFoundException;
}
Back to MVC AccountList