-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash_tables.java
80 lines (69 loc) · 2.04 KB
/
Hash_tables.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
import java.util.ArrayList;
public class Hash_tables {
private int size = 7;
private Node[] dataMap;
public Hash_tables(){
dataMap = new Node[size]; // array
}
public class Node{
private String key;
private int value;
private Node next;
public Node(String key, int value){
this .key = key;
this.value = value;
}
}
public void printTable(){
for(int i =0;i<dataMap.length;i++){
System.out.println(i + ":");
Node temp = dataMap[i];
while(temp != null){
System.out.println("{" +temp.key + ":" + temp.value + "}");
temp = temp.next;
}
}
}
private int hash(String key){
int hash =0;
char[] keychars= key.toCharArray();//array - tochararry - putting in array
for(int i =0;i<keychars.length;i++){
int asciivalue = keychars[i];
hash = (hash+asciivalue *23)% dataMap.length;//23 is prime no so number we get is random , datmap length is 7
}
return hash;
}
public void set(String key,int value){
int index = hash(key);
Node newNode = new Node(key, value);
if(dataMap[index] == null){
dataMap[index] =newNode;
}else{
Node temp = dataMap[index];
while(temp != null){
temp = temp.next;
}
temp.next = newNode;
}
}
public int get(String key){
int index = hash(key);
Node temp = dataMap[index];
while(temp != null){
if(temp.key == key){
return temp.value;
}temp = temp.next;
}return 0;
}
public ArrayList Keys(){
ArrayList<String> allkeys = new ArrayList<>();
for(int i = 0;i<dataMap.length;i++){
Node temp = dataMap[i];
while(temp != null){
allkeys.add(temp.key);
temp = temp.next;
}
}
return allkeys;
}
}