I have created my own custom HashMap for a special use-case, but I am missing the "clear" method.
I have only found one implementation of a clear method for HashMaps on the internet, but I simply cant wrap my head around it.
Can anyone help me? I have provided the custom HashMap and the one implementation i found on the internet.
My custom HashMap:
public class LongToOSMNodeMap {
private Node[] table;
int MASK;
public LongToOSMNodeMap(int capacity) {
table = new Node[1 << capacity]; // there are 2^{capacity} table cells
MASK = table.length - 1;
}
public void put(long id, double lon, double lat) {
int position = Long.hashCode(id) & MASK;
table[position] = new Node(id, lon, lat, table[position]);
}
public Node get(long id) {
int position = Long.hashCode(id) & MASK;
for (Node n = table[position]; n != null; n = n.next) {
if (n.id == id) {
return n;
}
}
return null;
}
public void clear() {
}
class Node extends OSMNode {
long id;
Node next;
public Node(long id, double lon, double lat, Node n) {
super(lon, lat);
this.id = id;
this.next = n;
}
}
Clear method implementation that i dont get, but want to implement on my own:
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}