forked from TheAlgorithms/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
Latest commit
39 lines (32 loc) · 737 Bytes
/
Copy pathHashMap.java
File metadata and controls
39 lines (32 loc) · 737 Bytes
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
classHashMap {
privateinthsize;
privateLinkedList[] buckets;
publicHashMap(inthsize) {
buckets = newLinkedList[hsize];
for (inti = 0; i < hsize ; i++ ) {
buckets[i] = newLinkedList();
// Java requires explicit initialisaton of each object
}
this.hsize = hsize;
}
publicinthashing(intkey) {
inthash = key % hsize;
if(hash < 0)
hash += hsize;
returnhash;
}
publicvoidinsertHash(intkey) {
inthash = hashing(key);
buckets[hash].insert(key);
}
publicvoiddeleteHash(intkey) {
inthash = hashing(key);
buckets[hash].delete(key);
}
publicvoiddisplayHashtable() {
for (inti = 0;i < hsize ; i++) {
System.out.printf("Bucket %d :",i);
buckets[i].display();
}
}
}