-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashMap.java
More file actions
155 lines (133 loc) · 4.21 KB
/
Copy pathMyHashMap.java
File metadata and controls
155 lines (133 loc) · 4.21 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
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
import java.util.HashMap;
public class MyHashMap<K, V> {
private MyArray<LinkedList<Pair<K, V>>> array;
private final float LoadFactor = 0.75F;
public int getSize(){
int size = 0;
for(LinkedList<Pair<K, V>> ll: array){
if(ll != null)
size += ll.getSize();
}
return size;
}
public MyHashMap(int capacity){
array = new MyArray<LinkedList<Pair<K, V>>>(capacity);
for(int i=0; i < capacity; i++){
array.insert(null);
}
}
public int getHash(K key){
return key.hashCode() % array.getCapacity();
}
public V get(K key){
int index = getHash(key);
for(Pair<K,V> pair: array.getData(index)){
if(key.equals(pair.key)){
return pair.value;
}
}
System.out.print("Error: Key not found.");
return null;
}
public void put(K key, V value){
if(getSize() >= array.getCapacity() * LoadFactor){
int index = getHash(key);
boolean no_resize = false;
if(array.getData(index) != null){
// Index in use.
LinkedList<Pair<K, V>> linkedList = array.getData(index);
int pos = linkedList.searchList(new Pair<K, V>(key, value));
if(pos != -1){
// key in use.
no_resize = true;
}
}
if(!no_resize){
resize();
}
}
_put(key, value);
}
private void _put(K key, V value){
int index = getHash(key);
if(array.getData(index) == null){
// Index not used.
LinkedList<Pair<K, V>> newLinkedList = new LinkedList<>(null);
newLinkedList.insert(new Pair<K, V>(key, value));
array.update(index, newLinkedList);
}else{
// Index in use.
LinkedList<Pair<K, V>> linkedList = array.getData(index);
int pos = linkedList.searchList(new Pair<K, V>(key, value));
if(pos != -1){
// If same key, then replace a with b.
linkedList.update(pos, new Pair<K, V>(key, value));
}else{
linkedList.insert(new Pair<K, V>(key, value));
}
}
}
private void resize() {
MyArray<Pair<K,V>> pairArray = new MyArray<Pair<K,V>>(getSize());
for(LinkedList<Pair<K, V>> ll: array){
if(ll == null){
continue;
}
for(Pair<K,V> pair: ll){
pairArray.insert(pair);
}
}
MyArray<LinkedList<Pair<K, V>>> newArray = new MyArray<LinkedList<Pair<K, V>>>(array.getCapacity() * 2);
for(int i=0; i < newArray.getCapacity(); i++){
newArray.insert(null);
}
array = newArray;
for(Pair<K,V> pair: pairArray){
_put(pair.key, pair.value);
}
}
public boolean remove(K key){
int index = getHash(key);
int position = -1;
boolean keyExist = false;
for(Pair<K,V> pair: array.getData(index)){
position ++;
if(key.equals(pair.key)){
keyExist = true;
break;
}
}
if(keyExist){
array.getData(index).delete(position);
return true;
}
System.out.print("Error: Key not found.");
return false;
}
private class Pair<K, V>{
public K key;
public V value;
public Pair(K key, V value){
this.key = key;
this.value = value;
}
@Override
public boolean equals(Object obj) {
Pair<K, V> pb = (Pair<K, V>) obj;
if(key.equals(pb.key)){
return true;
}
return false;
}
}
public static void main(String[] args) {
MyHashMap<Integer, String> myHashMap = new MyHashMap<Integer, String>(3);
myHashMap.put(7,"sth");
myHashMap.put(8,"yuanchu");
myHashMap.put(9, "sth");
myHashMap.put(8,"yuanchu2");
System.out.print(myHashMap.getSize());
myHashMap.get(8);
myHashMap.remove(9);
}
}