HashSet
example from Friday's lecture, the one that uses separate chaining.
In particular, check out the debug
method in that file, because it is a helpful way to print out the entire hash table state.
If you want to read more about implementing a collection using an array, you can read this sample Building Java Programs Chapter 18 hashing (please do not distribute!).
hashCode
function is not exactly the same as ours, your output will display the sets/maps elements in a different order.
This does not mean that your output is wrong, just that your hash code algorithm is slightly different.
You do not need to match ours exactly.
The Output Comparison Tool has "Ignore exact order of lines" and "Ignore order of chars on each line" boxes you can check that will help you match our output more precisely.
IllegalStateException
saying that the simulation does not contain a given name? I look at the prior output and I do see that name in the simulator.
HashMap
and see this message, it probably means your HvZPlayer
has an improper equals
or hashCode
method, so the map/set cannot find them.
If you are using your own HashMap
, it probably means that you are not properly finding a key or value that should be in the map.
A common cause of this is when you are not comparing keys properly, like if you use ==
to compare them rather than .equals
.
Node
objects.
Is that okay? What should I do?
HashSet
for an example that casts properly.
You can also add @SuppressWarnings("unchecked")
above your method/constructor header to remove the error.
This is okay and acceptable style in this one particular case.
(Please don't use @SuppressWarnings
elsewhere, only in this particular situation.)
Your line of code to construct your array of nodes should look something like this:
elements = (Node[]) new HashMap.Node[10];
(If you are curious about exactly why these weird casts are needed, read this explanation about generic arrays by UW CSE Prof Dan Grossman.)
StringBuilder
in my toString
method?
String
, but we don't require it.
keySet
method?
HashSet
(the one from java.util
). Yes, it is okay to import java.util.Set
and java.util.HashSet
to do this.
keySet
method and use it to help me solve the other methods?
keySet
is a bulky method that makes a copy of all your keys. So it is very inefficient to call it as a helper in all the other methods.
containsKey
, get
, etc. method to help me solve the other methods?
Node
class?
Node
, but if you want to put one or two small methods in there to help you (like a toString
method or something), that is fine.