-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashFunctions.java
More file actions
50 lines (47 loc) · 1.33 KB
/
HashFunctions.java
File metadata and controls
50 lines (47 loc) · 1.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
package datastructure.hashtable;
/**
* The type Hash functions.
*/
class HashFunctions
{
/**
* Hash fold int.
*
* @param key the key
* @param chunkSize the chunk size
* @return the int
*/
public static int hashFold(int key, int chunkSize) // Define the size of each divided portion
{
System.out.println ("Key: "+ key);
String strKey = Integer.toString(key); // Convert integer into string for slicing
int hashVal = 0;
System.out.println("Chunks:");
for(int i = 0; i < strKey.length(); i+=chunkSize)
{
if(i + chunkSize < strKey.length())
{
System.out.println(strKey.substring(i,i+chunkSize));
hashVal += Integer.parseInt(strKey.substring(i,i+chunkSize));
}
else
{
System.out.println(strKey.substring(i,strKey.length()));
hashVal+= Integer.parseInt(strKey.substring(i,strKey.length()));
}
}
return hashVal;
}
/**
* Main.
*
* @param args the args
*/
public static void main( String args[] )
{
int key = 3456789;
int chunkSize = 2;
System.out.println("Hash Key: " + hashFold(key, chunkSize));
//try another case with different values
}
}