-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
97 lines (92 loc) · 2.33 KB
/
HashTable.java
File metadata and controls
97 lines (92 loc) · 2.33 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
import java.util.ArrayList;
public class HashTable {
private int size=7;
private Node[] datamap;
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 HashTable(){
datamap=new Node[size];
}
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();
for(int i=0;i<keychars.length;i++){
int asciivalue=keychars[i];
hash=(hash+asciivalue*23)%datamap.length;
}
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.next!=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;
}
public static void main(String args[]){
HashTable myHashTable=new HashTable();
myHashTable.printTable();
int val=myHashTable.hash("paint");
System.out.println(val);
myHashTable.set("cream",300);
myHashTable.set("eyeliner", 200);
myHashTable.set("compact",250);
myHashTable.set("lipstick",150);
myHashTable.set("eyeshadow",200);
myHashTable.printTable();
System.out.println(myHashTable.get("compact"));
System.out.println(myHashTable.get("mascarra"));
myHashTable.set("paints",20);
myHashTable.set("bolts", 40);
myHashTable.set("nails",100);
myHashTable.set("tile",50);
myHashTable.set("lumber",80);
myHashTable.printTable();
System.out.println(myHashTable.keys());
}
}