/
MyHashMap.java
161 lines (142 loc) · 4 KB
/
MyHashMap.java
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
158
159
160
161
package jdk;
import java.util.LinkedList;
import java.util.List;
/**
* 未涉及到扩容操作的 hashMap
*/
public class MyHashMap {
public static void main(String args[]) {
MyHashMap myHashMap = new MyHashMap();
myHashMap.put("a", 123);
myHashMap.put("a", 123);
myHashMap.put("b", 345);
myHashMap.put(null, 34545);
System.out.println(myHashMap.size);
System.out.println(myHashMap.get("a"));
System.out.println(myHashMap.get(null));
}
private static int default_length = 999;
private int size = 0;
private Object[] arr = new Object[default_length];
/**
* 放入键值对
*
* @param key
* @param value
*/
public void put(Object key, Object value) {
int index = hash(key);
if (arr[index] == null) {
// 如果当前槽位是空的,直接放进去就可以了
List linkedList = new LinkedList();
linkedList.add(new MyEntry(key, value));
arr[index] = linkedList;
} else {
//如果不为空,检查当前 key 是否已存在
//如果已经存在,替换 value, 否则在链表头部插入新的元素
if (containsKey(key, index)) {
LinkedList linkedList = (LinkedList) arr[index];
for (Object o : linkedList) {
MyEntry entry = (MyEntry) o;
if (entry.getKey().equals(key)) {
entry.setValue(value);
}
}
return;
} else {
//链表头部插入元素
((LinkedList) arr[index]).add(0, new MyEntry(key, value));
}
}
size++;
}
/**
* hashMap 中指定位置是否包含指定key
* @param key
* @param index
* @return
*/
private boolean containsKey(Object key, int index) {
Object o = arr[index];
if (o == null) {
return false;
}
LinkedList linkedList = (LinkedList) o;
for (Object o1 : linkedList) {
if (o1 == null && key == null) {
return true;
}
MyEntry entry = (MyEntry) o1;
if (entry.getKey().equals(key)) {
return true;
}
}
return false;
}
/**
* 根据 key 获取 value
* 如果 key==null,特殊处理,放在了第一个位置
* 获得 key 的 hash 值,就确定了在 数组中的位置.
* 遍历该链表
* @param key
* @return
*/
public Object get(Object key) {
if (key == null) {
return ((MyEntry) ((LinkedList) arr[0]).getFirst()).getValue();
}
int index = hash(key);
Object o = arr[index];
if (o == null) {
return null;
}
LinkedList linkedList = (LinkedList) o;
for (Object o1 : linkedList) {
if (o1 == null) {
return null;
}
MyEntry entry = (MyEntry) o1;
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}
/**
* 给定一个对象计算其 hash 值
*
* @param key
* @return
*/
private int hash(Object key) {
if (key == null) {
return 0;
}
return key.hashCode() & default_length;
}
/**
* 内部类用来存放键值对
*/
class MyEntry {
private Object key;
private Object value;
private MyEntry() {
}
private MyEntry(Object key, Object value) {
this.key = key;
this.value = value;
}
public Object getKey() {
return key;
}
public void setKey(Object key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
}