-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashMap.java
More file actions
59 lines (51 loc) · 1.52 KB
/
MyHashMap.java
File metadata and controls
59 lines (51 loc) · 1.52 KB
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
package linear;
import java.util.LinkedList;
class MyHashMap {
// 연결리스트로 노드 객체 생성
private LinkedList<Entry>[] node;
// Entry -> 키-값 쌍을 저장하는 클래스
private static class Entry {
int key;
int value;
Entry(int key, int value) {
this.key = key;
this.value = value;
}
}
public MyHashMap() {
node = new LinkedList[1000000];
}
public void put(int key, int value) {
int index = key % node.length;
if (node[index] == null) {
node[index] = new LinkedList<>();
}
for (Entry entry : node[index]) {
if (entry.key == key) {
// 키가 존재하면 값을 업데이트
entry.value = value;
return;
}
}
node[index].add(new Entry(key, value)); // 새로운 키-값 쌍을 추가
}
public int get(int key) {
int index = key % node.length;
LinkedList<Entry> entries = node[index];
if (entries != null) {
for (Entry entry : entries) {
if (entry.key == key) {
return entry.value;
}
}
}
return -1; // 키에 대한 매핑이 없는 경우
}
public void remove(int key) {
int index = key % node.length;
LinkedList<Entry> entries = node[index];
if (entries != null) {
entries.removeIf(entry -> entry.key == key);
}
}
}