Java Using Eclipse The fact that Java has a HashMap class means that no Java pro
ID: 3589940 • Letter: J
Question
Java Using Eclipse
The fact that Java has a HashMap class means that no Java programmer has to write an implementation of hash tables from scratch
For this exercise, you should write a hash table in which both the keys and the values are of type String. (This is not an exercise in generic programming; do not try to write a generic class.) Write an implementation of hash tables from scratch. Define the following methods: get(key), put(key,value), remove(key), containsKey(key), and size(). Remember that every object, obj, has a method obj.hashCode() that can be used for computing a hash code for the object, so at least you don't have to define your own hash function. Do not use any of Java's built-in generic types; create your own linked lists using nodes. However, you do not have to worry about increasing the size of the table when it becomes too full.
You should also write a short program to test your solution.
Explanation / Answer
Hii..Please find below code to customhasmap with two classes. Please compile and execute the code.
HashMapCustom.java
class HashMapCustom<K, V> {
private Entry<K,V>[] table; //Array of Entry.
private int capacity= 4; //Initial capacity of HashMap
static class Entry<K, V> {
K key;
V value;
Entry<K,V> next;
public Entry(K key, V value, Entry<K,V> next){
this.key = key;
this.value = value;
this.next = next;
}
}
@SuppressWarnings("unchecked")
public HashMapCustom(){
table = new Entry[capacity];
}
public void put(K newKey, V data){
if(newKey==null)
return; //does not allow to store null.
//calculate hash of key.
int hash=hash(newKey);
//create new entry.
Entry<K,V> newEntry = new Entry<K,V>(newKey, data, null);
//if table location does not contain any entry, store entry there.
if(table[hash] == null){
table[hash] = newEntry;
}else{
Entry<K,V> previous = null;
Entry<K,V> current = table[hash];
while(current != null){ //we have reached last entry of bucket.
if(current.key.equals(newKey)){
if(previous==null){ //node has to be insert on first of bucket.
newEntry.next=current.next;
table[hash]=newEntry;
return;
}
else{
newEntry.next=current.next;
previous.next=newEntry;
return;
}
}
previous=current;
current = current.next;
}
previous.next = newEntry;
}
}
public V get(K key){
int hash = hash(key);
if(table[hash] == null){
return null;
}else{
Entry<K,V> temp = table[hash];
while(temp!= null){
if(temp.key.equals(key))
return temp.value;
temp = temp.next; //return value corresponding to key.
}
return null; //returns null if key is not found.
}
}
public boolean remove(K deleteKey){
int hash=hash(deleteKey);
if(table[hash] == null){
return false;
}else{
Entry<K,V> previous = null;
Entry<K,V> current = table[hash];
while(current != null){ //we have reached last entry node of bucket.
if(current.key.equals(deleteKey)){
if(previous==null){ //delete first entry node.
table[hash]=table[hash].next;
return true;
}
else{
previous.next=current.next;
return true;
}
}
previous=current;
current = current.next;
}
return false;
}
}
public void display(){
for(int i=0;i<capacity;i++){
if(table[i]!=null){
Entry<K, V> entry=table[i];
while(entry!=null){
System.out.print("{"+entry.key+"="+entry.value+"}" +" ");
entry=entry.next;
}
}
}
}
private int hash(K key){
return Math.abs(key.hashCode()) % capacity;
}
public int size(){
int count=0;
for(int i=0;i<capacity;i++){
if(table[i]!=null){
Entry<K, V> entry=table[i];
while(entry!=null){
entry=entry.next;
count++;
}
}
}
return count;
}
}
Testing.java
public class Testing {
public static void main(String[] args) {
HashMapCustom<String, String> hashMapCustom = new HashMapCustom<String, String>();
hashMapCustom.put("21", "12");
hashMapCustom.put("25", "121");
hashMapCustom.put("30", "151");
hashMapCustom.put("33", "15");
hashMapCustom.put("35", "89");
System.out.println("key 21="
+ hashMapCustom.get("21"));
System.out.println("key 51="
+ hashMapCustom.get("51"));
System.out.print("Display Data : ");
hashMapCustom.display();
System.out.println("key 21 removed: "
+ hashMapCustom.remove("21"));
System.out.println("key 51 removed: "
+ hashMapCustom.remove("51"));
System.out.print("Display Data : ");
hashMapCustom.display();
}
}
Thank you...All the best
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.