Podcast Ch21e

Podcast Ch21e
• Title: HashMap Collection
• Description: Overview; Entry class;
accessing entries; updating entries
• Participants: Barry Kurtz
(instructor); John Helfert and Tobie
Williams (students)
• Textbook: Data Structures for Java;
William H. Ford and William R. Topp
The HashMap Collection
• The design of the HashMap collection is
similar to the implementation of TreeMap.
A HashMap is not ordered since the
position of elements depends on hashing
the keys. This affects the method
toString() which returns a listing of the
elements based on the iterator order.
The HashMap Collection
(continued)
• The HashMap class stores elements in a hash
table containing linked lists of Entry objects.
The inner class Entry contains key-value pairs.
The HashMap Collection
(continued)
• The inner class Entry implements the
Map.Entry interface which defines the methods
getKey(), getValue() and setValue(). A
toString() method returns a representation of an
entry in the format "key=value". The
constructor has arguments for each field in the
node.
Entry Class (partial listing)
static class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> next;
int hashValue;
// make a new entry with given key, value
Entry(K key, V value, int hashValue,
Entry<K,V> next) {
this.key = key;
this.value = value;
this.hashValue = hashValue;
this.next = next;
}
...
}
Accessing Entries in a
HashMap
• The methods get(), and containsKey() take
a key reference argument and must locate
a corresponding entry in the map.
– This task is performed by the private
HashMap method getEntry() which takes a
key as an argument, applies the hash function
to the key and searches the resulting list for a
key-value pair with the same key.
Accessing Entries in a
HashMap (continued)
// return a reference to the entry with the
// specified key if there is one in the
// hash map; otherwise, return null
public Entry<K,V> getEntry(K key) {
int index = (key.hashCode() &
Integer.MAX_VALUE) % table.length;
Entry<K,V> entry;
entry = table[index];
while (entry != null) {
if (entry.key.equals(key))
return entry;
entry = entry.next;
}
return null;
}
Accessing Entries in a
HashMap (concluded)
// returns the value that corresponds to
// the specified key
public V get(K key)
{
Entry<K,V> p = getEntry(key);
if (p == null)
return null;
else
return p.value;
}
Updating Entries in a HashMap
• The method put() updates the HashMap.
– Construct a table index by applying the hash
function for the key and scan the linked list for
a match with the key. If a match occurs, apply
setValue() and return its result.
– If key does not occur in the list, insert a new
Entry object at the front of the linked list. If the
hash map size has reached the table
threshold, apply rehashing. Conclude by
returning null.
Updating Entries in a HashMap
(continued)
// assigns value as the value associated with key
// in this map and returns the previous value
// associated with the key, or null if there
// was no mapping for the key
public V put(K key, V value) {
// compute the hash table index
int hashValue= key.hashCode()& Integer.MAX_VALUE,
index = hashValue % table.length;
Entry<K,V> entry;
// entry references the front of a linked
// list of colliding values
entry = table[index];
Updating Entries in a
HashMap (continued)
// scan the linked list. if key matches the key
// in an entry, return entry.setValue(value).
// this replaces the value in the entry and
// returns the previous value
while (entry != null) {
if (entry.key.equals(key))
return entry.setValue(value);
entry = entry.next;
}
// we will add item, so increment modCount
modCount++;
// create the new table entry so its successor
// is the current head of the list
entry = new Entry<K,V>(key, value, hashValue,
(Entry<K,V>)table[index]);
Updating Entries in a HashMap
(concluded)
// add it at the front of the linked list
// and increment the size of the hash map
table[index] = entry;
hashMapSize++;
if (hashMapSize >= tableThreshold)
rehash(2*table.length + 1);
return null;
}
// a new entry is inserted
Summary of HashMap Design