Due: Mon, Sep 23, by 11:59pm
|
Due: Thu, Sep 26, by 11:59pm
|
HashMap
data structure. The HashMap
can be viewed as a generalization of the primitive array or ArrayList
. We still use indices to store data in the HashMap
, but here the indices do not need to be sequential, and in fact, the indices do not need to be integers.
For a detailed explanation see the handout on Moodle and the example here:
Hash Map ExampleQuick summary:
Inserting in a HashMap
Calculate the bucket indexi
where the item would have been inserted:Searching in a HashMap
- Chaining: search the list/bucket for an entry with the given key -- if found, replace the value, otherwise add a new entry
- Linear Probing: search the buckets with indices
i, i+1, i+2, ...
until either an entry with the key is found (replace value) or an Empty marker is found (add a new entry in the cell that has the first Deleted marker encountered during the search)
Calculate the bucket indexi
where the item would have been inserted:Deleting from a HashMap
- Chaining: search the list/bucket for an entry with the given key
- Linear Probing: search the buckets with indices
i, i+1, i+2, ...
until either an entry with the key is found or an Empty marker is found
Calculate the bucket indexi
where the item would have been inserted:
- Chaining: search the list/bucket for an entry with the given key -- if found, remove the entry from the list/bucket; use while loop with iterator
- Linear Probing: search the buckets with indices
i, i+1, i+2, ...
until either an entry with the key is found or an Empty marker is found; if the item is found, put in the cell the Deleted marker (why?)
CHashMap
and LHashMap
that implement the methods given below using the Chaining and Linear Probing collision resolution schemes:
CHashMap
and LHashMap
, respectivelyLHashMap
and CHashMap
must keep track of the size (i.e. number of entries)null
to indicate "failure"null
keys are allowed, but do not include test cases for thisCHashMapTest
and LHashMapTest
, respectivelyKeep in mind that you will need to:
Entry
that stores the original key and value provided by the userHashMap
is parameterized on two types HashMap<K, V>
, i.e. similar to LinkedList
, but with two letters
This class has only two data members ( |
Setup
Use a primitive array for the buckets: |
|||||||||
?HashMap(int initialCapacity, double loadFactor)
Creates a map with the given capacity and load factor; make sure to put empty buckets in the hash map container (empty lists for Chaining, Empty marker for Linear Probing). The |
|||||||||
boolean isEmpty()
Determines if the map is empty; this method must run in time
|
|||||||||
int size()
Returns the number of |
|||||||||
V put(K key, V value)
Puts the given value under the given key and returns the old value associated with this key. |
|||||||||
V get(K key)
Returns the value associated with the given key. |
|||||||||
V remove(K key)
Removes/deletes the entry with the given key from the map and returns the entry's value. |
|||||||||
void clear()
clears the map |
|||||||||
boolean containsKey(K key)
Determines if the map contains an element with the given key/index. |
|||||||||
boolean containsValue(V value)
Determines if the map contains the given value.
For
|
|||||||||
void rehash()
(private) Expands the map storage (after adding new entry) if |
|||||||||
String toString()
Returns a string representation of the map. Here are examples of maps with capacity 10 -- one map has 4 entries and the other map is empty, i.e. it has no entries, but it does have buckets: |
|||||||||
Iterator<V> iterator()
Returns an iterator over the values stored in this map from the first bucket to the last; operation remove is not implemented. You are only expected to implement iterator for |
toString
String
class MyString.java for the keysMyString
overrides the hashCode
method so that it returns the length of the string:
MyString myCat = new MyString("cat"); myCat.hashCode() will give 3: nice and simple String javaCat = "cat"; javaCat.hashCode() will give 98262: not simple MyString otherCat = new MyString("4cat"); otherCat.hashCode() will give 43
// shortcut for making MyString objects // * instead of: myMap.put( new MyString("cat"), 4 ); // * can write: myMap.put( ms("cat"), 4 ); private static MyString ms(String str) { return new MyString(str); }
?HashMap<MyString, Integer> map = new ?HashMap<MyString, Integer>(...); assertEquals( map.toString(), "the expected contents" ); assertTrue( map.put( ms("january"), 31 ) == ??? ); assertEquals( map.toString(), "the expected contents" ); ... assertTrue( map.remove( ms("january") == ??? ); assertEquals( map.toString(), "the expected contents" ); ... assertTrue( map.put( ms("april"), 30 ) == ??? ); assertEquals( map.toString(), "the expected contents" ); ... assertTrue( map.remove( ms("april") == ??? ); assertEquals( map.toString(), "the expected contents" ); // // LHashMapTest: At the end test for "fail" conditions for iterator. //