-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHashMap.java
146 lines (124 loc) · 3.38 KB
/
HashMap.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
// import java.util.LinkedList;
public class HashMap {
private static final int INITIAL_CAPACITY = 10;
private static final double LOAD_FACTOR = 0.75;
private Entry[] table;
private int size;
private int capacity;
// 哈希表的元素结构体
public class Entry {
String key;
int value;
Entry next; // 用于链表解决冲突
public Entry(String key, int value) {
this.key = key;
this.value = value;
this.next = null;
}
}
// 初始化 HashMap
public HashMap() {
this.size = 0;
this.capacity = INITIAL_CAPACITY;
this.table = new Entry[this.capacity];
}
// 哈希函数
private int hash(String key) {
int hashValue = 0;
for (int i = 0; i < key.length(); i++) {
hashValue = (hashValue * 31) + key.charAt(i);
}
return Math.abs(hashValue) % this.capacity;
}
// 扩容
private void resize() {
int newCapacity = this.capacity * 2;
Entry[] newTable = new Entry[newCapacity];
// 迁移原表的数据到新表
for (int i = 0; i < this.capacity; i++) {
Entry entry = this.table[i];
while (entry != null) {
int newIndex = hash(entry.key) % newCapacity;
Entry newEntry = new Entry(entry.key, entry.value);
newEntry.next = newTable[newIndex];
newTable[newIndex] = newEntry;
entry = entry.next;
}
}
this.capacity = newCapacity;
this.table = newTable;
}
// 插入键值对(如果存在则更新)
public void put(String key, int value) {
// 判断是否需要扩容
if ((double) this.size / this.capacity > LOAD_FACTOR) {
resize();
}
int index = hash(key);
Entry entry = this.table[index];
// 遍历链表,检查是否已经存在相同的键
while (entry != null) {
if (entry.key.equals(key)) {
entry.value = value; // 更新值
return;
}
entry = entry.next;
}
// 如果没有找到相同的键,插入新键值对
Entry newEntry = new Entry(key, value);
newEntry.next = this.table[index];
this.table[index] = newEntry;
this.size++;
}
// 查找键
public int get(String key) {
int index = hash(key);
Entry entry = this.table[index];
while (entry != null) {
if (entry.key.equals(key)) {
return entry.value;
}
entry = entry.next;
}
return -1; // 返回 -1 表示未找到
}
// 删除键
public void delete(String key) {
int index = hash(key);
Entry entry = this.table[index];
Entry prev = null;
while (entry != null) {
if (entry.key.equals(key)) {
if (prev == null) {
this.table[index] = entry.next;
} else {
prev.next = entry.next;
}
this.size--;
return;
}
prev = entry;
entry = entry.next;
}
}
// 测试
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 30);
System.out.println("apple: " + map.get("apple"));
System.out.println("banana: " + map.get("banana"));
System.out.println("grape: " + map.get("grape"));
map.delete("banana");
System.out.println("banana after delete: " + map.get("banana"));
}
}
/*
* jarry@MacBook-Pro hash % javac HashMap.java
* jarry@MacBook-Pro hash % java HashMap
* apple: 10
* banana: 20
* grape: -1
* banana after delete: -1
*/