Lab 08: HashMap
Implementing a HashMap, wooohoooo.
Prerequisites to running the code.
Instructions: https://fa22.datastructur.es/materials/lab/lab08/
Solution: https://github.com/tomthestrom/cs61b/tree/master/lab08/hashmap
HashMap
Implementation Requirements #
You should implement the following constructors:
public MyHashMap();
public MyHashMap(int initialSize);
public MyHashMap(int initialSize, double loadFactor);Some additional requirements for MyHashMap are below:
Your hash map should initially have a number of buckets equal to
initialSize. You should increase the size of yourMyHashMapwhen the load factor exceeds the setloadFactor. Recall that the load factor can be computed asloadFactor = N/M, whereNis the number of elements in the map andMis the number of buckets. The load factor represents the amount of elements per bucket, on average. IfinitialSizeandloadFactoraren’t given, you should set defaultsinitialSize = 16andloadFactor = 0.75(as Java’s built-in HashMap does)You should handle collisions with separate chaining. You may not import any libraries other than
ArrayList,LinkedList,Collection,HashSet,iteratorandSet. This means that forMyHashMap.java, you should use start withLinkedListfor the bucket type inMyHashMap.java. For more detail on how you should implement separate chaining, see the Skeleton Code setion above.Because we use a
Collection<Node>[]as ourbuckets, when implementingMyHashMap, you are restricted to using methods that are supported by theCollectioninterface. The only methods you will need areadd,remove, anditerator. When you are searching for aNodein aCollection, simply iterate over theCollection, and find theNodewhosekeyis.equal()to the key you are searching forWhen resizing, make sure to multiplicatively resize, not additively resize. You are not required to resize down
Your
MyHashMapoperations should all be constant amortized time, assuming that thehashCodeof any objects inserted spread things out nicely (recall: everyObjectin Java has its ownhashCode()method). Note:hashCode()can return a negative value! Please take this into account when writing your code. Take a look at the lecture slides linked below for a hint on how to cleanly handle this case.If the same key is inserted more than once, the value should be updated each time. You can assume
nullkeys will never be inserted.
Implementation
About
The task was to implement MyHashMap, so that it could use different types of data structures for buckets through inheritance. The CS61B team had already provided classes extending MyHashMap, such as MyHashMapLLBuckets, MyHashMapPQBuckets, etc.
It was cool to implement a hash map, though it's hard to write a lot about it, the specifications were pretty straightforward and it wasn't a lot of code.
The default load factor was 0.75, though I provided a constructor as required by the specs, (one of three constructors) to be able to use a different load factor.
The load factor is basically the ratio of elements inside the hashmap divided by the amount of buckets. This, coupled with having a good hash function, that is able to evenly distribute the elements across the buckets, ensures that we have a constant time of accessing its elements (except for when resize operation occurs).
Code
Last updated