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 your MyHashMap when the load factor exceeds the set loadFactor. Recall that the load factor can be computed as loadFactor = N/M, where N is the number of elements in the map and M is the number of buckets. The load factor represents the amount of elements per bucket, on average. If initialSize and loadFactor aren’t given, you should set defaults initialSize = 16 and loadFactor = 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, iterator and Set. This means that for MyHashMap.java, you should use start with LinkedList for the bucket type in MyHashMap.java. For more detail on how you should implement separate chaining, see the Skeleton Code setion above.

  • Because we use a Collection<Node>[] as our buckets, when implementing MyHashMap, you are restricted to using methods that are supported by the Collection interface. The only methods you will need are add, remove, and iterator. When you are searching for a Node in a Collection, simply iterate over the Collection, and find the Node whose key is .equal() to the key you are searching for

  • When resizing, make sure to multiplicatively resize, not additively resize. You are not required to resize down

  • Your MyHashMap operations should all be constant amortized time, assuming that the hashCode of any objects inserted spread things out nicely (recall: every Object in Java has its own hashCode() 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 null keys 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