-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHashTable.java
95 lines (81 loc) · 1.96 KB
/
HashTable.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
import java.util.LinkedList;
class HashTable {
private static final int TABLE_SIZE = 10;
private LinkedList<Entry>[] table;
@SuppressWarnings("unchecked")
public HashTable() {
table = new LinkedList[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++) {
table[i] = new LinkedList<>();
}
}
private int hash(int key) {
return key % TABLE_SIZE;
}
public void insert(int key, int value) {
int index = hash(key);
for (Entry entry : table[index]) {
if (entry.key == key) {
entry.value = value; // 更新值
return;
}
}
table[index].add(new Entry(key, value));
}
public int search(int key) {
int index = hash(key);
for (Entry entry : table[index]) {
if (entry.key == key) {
return entry.value;
}
}
return -1; // 键未找到
}
public void delete(int key) {
int index = hash(key);
table[index].removeIf(entry -> entry.key == key);
}
public void printTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
System.out.print("Index " + i + ": ");
for (Entry entry : table[i]) {
System.out.print("[" + entry.key + ":" + entry.value + "] ");
}
System.out.println();
}
}
static class Entry {
int key;
int value;
Entry(int key, int value) {
this.key = key;
this.value = value;
}
}
public static void main(String[] args) {
HashTable hashTable = new HashTable();
hashTable.insert(1, 100);
hashTable.insert(2, 200);
hashTable.insert(3, 300);
System.out.println("Search key 2: " + hashTable.search(2));
hashTable.delete(2);
System.out.println("After deleting key 2:");
hashTable.printTable();
}
}
/*
* jarry@MacBook-Pro hash % javac HashTable.java
* jarry@MacBook-Pro hash % java HashTable
* Search key 2: 200
* After deleting key 2:
* Index 0:
* Index 1: [1:100]
* Index 2:
* Index 3: [3:300]
* Index 4:
* Index 5:
* Index 6:
* Index 7:
* Index 8:
* Index 9:
*/