please do the get , put , and rehash methods. r Linear Probing Hash Table public
ID: 3830365 • Letter: P
Question
please do the get, put, and rehash methods.
Explanation / Answer
Hashtable.java
public class Hashtable {
private int currentSize, maxSize;
public Pair[] map=null;
//Cunstructor
public Hashtable(int capacity){
currentSize=0;
maxSize= capacity;
map=new Pair[maxSize];
for(int i=0;i<maxSize;i++)
map[i]=null;
}
//Function to check if hash table is empty
public boolean isEmpty(){
return getSize()==0;
}
//Function to get size of the hash table
public int getSize() {
return currentSize;
}
//Function to check if hash table is full
public boolean isFull(){
return currentSize==maxSize;
}
//Function to check if hastable contains a key
public boolean contains(String key){
return get(key)!=null;
}
//Function to get hash-code/hash value for a given key
public int hash(String key){
return Math.abs(key.hashCode())%maxSize;
}
//Function to get value for a given key
public String get(String key){
//Get element value using key and linear probing if the element doesnot exist return null
for (Pair pair : map) {
if(pair.getKey().equals(key))
return pair.getValue();
}
return null;
}
//Functon to insert key-value pair
public void put(String key, String val){
//insert element val using key, If the table is full you must call rehash first
Pair pair=new Pair(key,val);
if(isFull())
rehash();
map[currentSize]=pair;
currentSize++;
}
//Function to rehash when the table is full
private void rehash(){
//backup the reference to the old table
Pair[] map1=map;
//create new map twice the size of the old size
map=new Pair[currentSize*2];
maxSize=currentSize*2;
//hash all the elements from the old hash to new hash map
for (int i=0;i<currentSize;i++) {
map[i]=map1[i];
}
}
//Function to print HashTable
public void printHashTable(){
System.out.println(" Hash Table: Key, Value ");
for(int i=0;i<maxSize;i++){
if(map[i]!=null)
System.out.println(map[i].getKey()+", "+map[i].getValue());
}
System.out.println();
}
}
Pair.java
public class Pair {
private String key;
private String value;
public Pair(String key, String value){
this.key=key;
this.value=value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
HashTest.java
public class HashTest {
public static void main(String[] args) {
Hashtable myTable=new Hashtable(2);
System.out.println("Is table Empty "+myTable.isEmpty());
myTable.put("Key_1", "Value_1");
myTable.put("Key_2", "Value_2");
System.out.println("Get value of Key_2: "+myTable.get("Key_2"));
System.out.println(" Is table Empty: "+myTable.isEmpty());
System.out.println("Size of the table: "+myTable.getSize());
myTable.printHashTable();
myTable.put("Key_3","Value_3");
myTable.put("Key_4", "Vlaue_4");
System.out.println("Get value of Key_2: "+myTable.get("Key_2"));
System.out.println("Size of the table: "+myTable.getSize());
myTable.printHashTable();
}
}
Sample Output:
Is table Empty true
Get value of Key_2: Value_2
Is table Empty: false
Size of the table: 2
Hash Table:
Key, Value
Key_1, Value_1
Key_2, Value_2
Get value of Key_2: Value_2
Size of the table: 4
Hash Table:
Key, Value
Key_1, Value_1
Key_2, Value_2
Key_3, Value_3
Key_4, Vlaue_4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.