Design HashMap

Design a HashMap without using any built-in hash table libraries.

To be specific, your design should include these functions:

  • put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
  • get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.


Example:

MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);          
hashMap.put(2, 2);         
hashMap.get(1);            // returns 1
hashMap.get(3);            // returns -1 (not found)
hashMap.put(2, 1);          // update the existing value
hashMap.get(2);            // returns 1 
hashMap.remove(2);          // remove the mapping for 2
hashMap.get(2);            // returns -1 (not found) 

 1 class MyHashMap {
 2     final ListNode[] nodes = new ListNode[10000];
 3 
 4     public void put(int key, int value) {
 5         int i = idx(key);
 6         ListNode first = nodes[i];
 7         ListNode newNode = new ListNode(key, value);
 8         if (first == null) {
 9             nodes[i] = newNode;
10         } else {
11             ListNode sameNode = find(nodes[i], key);
12             if (sameNode == null) {
13                 newNode.next = first;
14                 nodes[i] = newNode;
15             } else {
16                 sameNode.val = value;
17             }
18         }
19     }
20 
21     public int get(int key) {
22         int i = idx(key);
23         if (nodes[i] == null) {
24             return -1;
25         }
26         ListNode node = find(nodes[i], key);
27         return node == null ? -1 : node.val;
28     }
29 
30     public void remove(int key) {
31         int i = idx(key);
32         if (nodes[i] == null) {
33             return;
34         }
35         ListNode current = nodes[i];
36         ListNode previous = null;
37         while (current != null) {
38             if (current.key == key) {
39                 if (previous != null) {
40                     previous.next = current.next;
41                 } else {
42                     nodes[i] = current.next;
43                 }
44                 break;
45             } else {
46                 previous = current;
47                 current = current.next;
48             }
49         }
50     }
51 
52     int idx(int key) {
53         return Integer.hashCode(key) % nodes.length;
54     }
55 
56     ListNode find(ListNode node, int key) {
57         while (node != null) {
58             if (node.key == key) {
59                 return node;
60             }
61             node = node.next;
62         }
63         return null;
64     }
65 
66     class ListNode {
67         int key, val;
68         ListNode next;
69 
70         ListNode(int key, int val) {
71             this.key = key;
72             this.val = val;
73         }
74     }
75 }

 



posted @ 2019-07-15 10:54  北叶青藤  阅读(203)  评论(0编辑  收藏  举报