
Due: Mon, Feb 16, by 11:59pm
|
Due: Thu, Feb 19, by 11:59pm
|
CHashMap and LHashMap. These will have the files mentioned above. The Entry class is the same for both, so you can copy from one project to the other.
The CHashMap may be easier to start with.
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 indexiwhere 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 indexiwhere 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 indexiwhere 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 this and do not check if the user sent null keyCHashMapTest 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<E>, but with two letters K and V
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 |
|||||||||
V put(K key, V value)
Puts the given value under the given key and returns the old value associated with this key. (For |
|||||||||
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. |
|||||||||
boolean containsValue(V value)
Determines if the map contains the given value.
For
|
|||||||||
void rehash()
(private, |
|||||||||
String toString()
These should be short methods. No |
|||||||||
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 |
toStringString 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" );