Untitled
unknown
java
10 months ago
4.1 kB
25
Indexable
class Constants {
public static final int REM = 7;
public static final int SIZE = 20;
}
class Pair{
String key;
String value;
Pair(String key , String value){
this.key = key;
this.value = value;
}
}
class CustomHashMap{
//we will use an array to store the key value pairs
Pair[] array;
//List<Pair> array;
//Every hashmap/hashtable internally it is an array so we might as well initialise it when we call our hashmap constructor
CustomHashMap(){
array = new Pair[Constants.SIZE];
}
/* lets start with insert function
we hash the key , we use the key as an index to place the value in an array
now what if there is a collision , means what if there are two entirely different strings that produce the same value ?
looks like we have a problem , to address these collisons
we have two methods , open addressing and chaining
in the present approach we go with open addressing as it is simple to understand
in open addressing we go for linear probing means Mainhe same key
and there is already an existing string in the key index, we go for the next index and place our string there
*/
public void put(String key , String value) throws Exception{
int hashkey = hash(key);
//if we hit a collision
if(array[hashkey]!=null){
int i = hashkey;
//we go for linear probing
while(array[i]!=null){
// If key already exists, update its value
if (array[i].key.equals(key)) {
array[i]= new Pair(key, value);
return;
}
i =(i+1)%Constants.SIZE;
/*if we reached the same position where we have
started then we have no space left anywhere in the array
*/
if(i==hashkey){
try {
throw new Exception("No space left in the array");
} catch (Exception e) {
e.printStackTrace();
}
}
}
array[i]= new Pair(key,value);
}
else{
array[hashkey]=new Pair(key,value);
}
}
/*
now we will proceed with the retrieve function ,
it takes the key hashes it and searches for the element
if there is already an exising element we search the array
in a circular format , just like how we inserted when we
have collisions
*/
public String get(String key){
int hashkey = hash(key);
int i = hashkey;
while(array[i]!=null){
// search until , the given key and they key in the pair is equal
if(array[i].key.equals(key)){
return array[i].value;
}
i = (i+1)%Constants.SIZE;
if(i==hashkey){ // if we reach the same position we started then we have searched the entire array
return null;
}
}
return null;
}
//for hashing for strings I'm just summing up all the characters with their ASCII value and reducing the sum with a remainder
private int hash(String key){
int value =0;
for(int i=0;i<key.length();i++){
value += key.charAt(i);
}
return value%Constants.REM;
}
}
public class Main {
public static void main(String[] args) throws Exception {
CustomHashMap map = new CustomHashMap();
map.put("Sherlock", "Detective");
map.put("Moriarity", "Criminal");
map.put("Watson", "Doctor");
System.out.println(map.get("Sherlock"));
System.out.println(map.get("Moriarity"));
System.out.println(map.get("Watson"));
System.out.println(map.get("Lestrade"));
}
}Editor is loading...
Leave a Comment