0% found this document useful (0 votes)
13 views

Class Notes: Implementing Hashmaps (Implementation View) Today Is Also Pencil/paper

The document discusses implementing a hashmap data structure. It provides an example of adding keys and values to a hashmap, and shows how collisions are handled by storing multiple key-value pairs in each array slot. The key insights are that from a programmer's perspective there can only be one value per key, but under the hood multiple keys may map to the same array slot, with their entries stored in a linked list. The homework will be to implement a hashmap class that supports basic dictionary operations like lookup, insert, update and delete.

Uploaded by

Alexis Lopez
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Class Notes: Implementing Hashmaps (Implementation View) Today Is Also Pencil/paper

The document discusses implementing a hashmap data structure. It provides an example of adding keys and values to a hashmap, and shows how collisions are handled by storing multiple key-value pairs in each array slot. The key insights are that from a programmer's perspective there can only be one value per key, but under the hood multiple keys may map to the same array slot, with their entries stored in a linked list. The homework will be to implement a hashmap class that supports basic dictionary operations like lookup, insert, update and delete.

Uploaded by

Alexis Lopez
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Class Notes: Implementing Hashmaps

(Implementation View)

today is also pencil/paper


Conceptual Hat
Remember this picture from the last class:
hashCode
kfisler n 0 Customer 000003 0 Account
evanv m 1 “blueno” 104219 1 999998
putdam x 2 222112 2
ngoodson y 3 Customer 327112 3 Account
pzubiago z 4
“peter” 415832 4
415832
blueno w 5 999998 5
% %
mod Customer mod Account
Programmer shouldn’t see gray part
“kathi” Programmer shouldn’t see gray part
000003

import java.util.HashMap;

private HashMap<Integer,Account> accounts = new HashMap<Integer, Account>();


private HashMap<String,Customer> customers = new HashMap<String, Customer>();

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));

What questions does this exercise bring up for you?


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
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;

// Updates the value associated with the given key.


// throws KeyNotFoundException if the key is not found
public V update(K key, V value) throws KeyNotFoundException;

// Inserts a key-value pair into the dictionary.


// throws KeyAlreadyExistsException if the key already exists
public void insert(K key, V value) throws
KeyAlreadyExistsException; class KVPair<K,V> {
public K key;
// Deletes a key-value pair from the dictionary.public V value;
}
// throws KeyNotFoundException if the key is not found
public V delete(K key) throws KeyNotFoundException;
class HashTable<K,V> {
} int size; // number of slots
LinkedList<KVPair<K,V>> contents;
}
Having hashcodes and Conceptual Hat
Hashtable Runtimes array size be relatively
prime is a good practice Good runtime needs
(see PDF notes)
hashCode distribution of values
kfisler n 0
across array slots and
evanv m 1 LinkedList<KVPair>
enough slots to enable
putdam x 2
shorter lists
ngoodson y 3
pzubiago z 4 LinkedList<KVPair>
blueno w 5
%
mod

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

BankingConsole BankingService AccountHashTable


Solution has two parts:
(1) Take specific empty data
CustomerList structures as constructor
inputs (in BankingService)

public class Main {


public static void main(String[] args) { (2) Have an interface with
BankingService controller = new BankingService(); needed operations on each
of Accounts and Customers
BankingConsole view = new BankingConsole(controller); // blue arrow!
... data structures
}

public class BankingService { The choice of data structure is


AccountList accounts = new AccountList(); // solid red arrows currently FIXED in the
CustomerList customers = new CustomerList(); BankingService – how could
we customize the data
structure without editing the
BankingService class?

You might also like