1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Arrays; import java.util.HashMap;
public class MyHashMap {
private static class Node { int key, val; int hashcode; Node next;
public Node(int key, int val, int hashcode) { this.key = key; this.val = val; this.hashcode = hashcode; this.next = null; }
@Override public String toString() { return "Node{" + "key=" + key + ", val=" + val + ", hashcode=" + hashcode + ", next=" + next + '}'; } }
private int DEFAULT_CAPACITY = 16;
private Node[] nodes; private int size; private double factor = 0.75; private int threshold = (int) (DEFAULT_CAPACITY * factor);
public MyHashMap() { this.nodes = new Node[16];
}
public void put(int key, int value) { int hashcode = Integer.hashCode(key); int index = hashcode & (this.nodes.length - 1); if (!headPut(index, hashcode, key, value, nodes)) return; if (++size >= threshold) grow(); }
private boolean headPut(int index, int hashcode, int key, int value, Node[] nodes) { Node tmp = nodes[index]; while (tmp != null) { if (tmp.key == key) { tmp.val = value; return false; } tmp = tmp.next; } Node newHead = new Node(key, value, hashcode); newHead.next = nodes[index]; nodes[index] = newHead; return true; }
private void grow() { int newCapacity = this.nodes.length << 1; int newThreshold = (int) (newCapacity * this.factor); int oldCapacity = this.nodes.length;
Node[] newNodes = new Node[newCapacity]; this.threshold = newThreshold; for (int i = 0; i < oldCapacity; i++) { Node old = this.nodes[i]; while (old != null) { int newIndex = (old.hashcode & oldCapacity) == 0 ? i : i + oldCapacity; headPut(newIndex, old.hashcode, old.key, old.val, newNodes); old = old.next; } this.nodes[i] = null; } this.nodes = newNodes; }
public int get(int key) { int hashcode = Integer.hashCode(key); int index = hashcode & (this.nodes.length - 1);
Node tmp = this.nodes[index];
while (tmp != null) { if (tmp.key == key) return tmp.val; tmp = tmp.next; } return -1; }
public void remove(int key) { int hashcode = Integer.hashCode(key); int index = hashcode & (this.nodes.length - 1);
Node tmp = this.nodes[index];
if (tmp == null) return; if (tmp.key == key) { this.nodes[index] = tmp.next; --size; return; }
Node mv = tmp.next; while (mv != null) { if (mv.key == key) { tmp.next = mv.next; --size; return; } mv = mv.next; tmp = tmp.next; } }
public static void main(String[] args) throws IOException { MyHashMap map = new MyHashMap(); Writer out = new BufferedWriter(new OutputStreamWriter(System.out)); out.write("[null,"); map.put(504, 155); map.remove(89); out.write("," + String.valueOf(map.get(334))); out.flush(); } }
|