Finally, we will implement a few utility methods from the Map
interface that are useful for working with our MyHashMap
.
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 the hashtable
and add all of the SimpleEntry<K, V>
entires from the current LinkedList
to the HashSet
using the HashSet
’s addAll
method.
Return the HashSet
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.
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
.
Do not forget to write unit tests verifying that your entrySet, containsKey and containsValue work as intended.
Don’t forget to frequently save your progress to GitHub using the add
/commit
/push
commands with git.