Map Methods
Finally, we will implement a few utility methods from the Map
interface that are useful for working with our MyHashMap
.
entrySet Method
We might want to be able to iterate over every SimpleEntry<K, V>
saved in the MyHashMap
. Since these entries are unique and have no order (since keys have no order), we typically store the collection of SimpleEntry<K, V>
as a Set
. Implement a method:
public Set<SimpleEntry<K, V>> entrySet()
that:
Creates a new empty
HashSet<SimpleEntry<K, V>>
Iterate over every
LinkedList
of thehashtable
and add all of theSimpleEntry<K, V>
entries from the currentLinkedList
to theHashSet
using theHashSet
’saddAll
method.Return the
HashSet
containsKey Method
A common method that is used with Map
s within applications is deciding whether a given key
is already stored in the Map
. Implement a method
public boolean containsKey(K key)
that returns true
if key
is already contained in the MyHashMap
, else false
.
Hint
Our find
helper method implemented in Part 2 should be really helpful again here.
containsValue Method
Another common method that is used with Map
s within applications is deciding whether a given value
is already stored in the Map
. Implement a method
public boolean containsValue(V value)
that returns true
if value
is already contained in at least one in SimpleEntry<K, V>
in the MyHashMap
, else false
.
Hint
Unfortunately, our find
helper method is not helpful here. Instead, we should iterate over every SimpleEntry<K, V>
in every LinkedList
of the hashtable
and return true
if the getValue()
of any SimpleEntry<K, V>
equals()
the given value
.
Testing Our Progress
Do not forget to write unit tests verifying that your entrySet, containsKey and containsValue work as intended.
Committing Your Progress
Don’t forget to frequently save your progress to GitHub using the add
/commit
/push
commands with git.