forked from rbk-org/Java_DataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
64 lines (55 loc) · 1.75 KB
/
Copy pathHashTable.java
File metadata and controls
64 lines (55 loc) · 1.75 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
import java.util.LinkedList;
public class HashTable {
public static final int ARR_SIZE = 8;
// public int counter=0;
public LinkedList<Bucket>[] arr = new LinkedList[ARR_SIZE];
//hash function
public int getIndexBelowMaxForKey(String str, int max) {
int hash = 0;
for (int i = 0; i < str.length(); i++) {
hash = (hash << 5) + hash + str.charAt(i);
hash = hash & hash; // Convert to 32bit integer
hash = Math.abs(hash);
}
return hash % max;
}
public static class Bucket {
//your code is here
public String value;
public String key;
}
public void insert (String key, String value) {
//your code is here
boolean isthere =false;
int index = getIndexBelowMaxForKey(key, ARR_SIZE);
Bucket bucket=new Bucket();
bucket.key=key;
bucket.value=value;
if(arr[index]==null){
LinkedList lk =new LinkedList();
lk.add(bucket);
arr[index]=lk;
}else if(arr[index]!=null){
LinkedList lk = arr[index];
for (int i = 0; i < lk.size(); i++) {
Bucket buc = (Bucket) lk.get(i);
if(buc.key==key){
isthere = true;
buc.value=value;
lk.remove(i);
lk.add(buc);
}
}
if(!isthere) lk.add(bucket);
arr[index] =lk;
}
}
// public String retrieve(String key) {
// // your code is here
// int index = getIndexBelowMaxForKey(key, ARR_SIZE);
// }
public void remove (String key) {
//your code is here
int index = getIndexBelowMaxForKey(key, ARR_SIZE);
}
}