HOW TO IMPLEMENT MY HASH TABLE IN JAVA?

 public class HashEntry {  
    private int key;  
    private int value;  
    HashEntry(int key, int value) {  
       this.key = key;  
       this.value = value;  
    }     
     public int getKey(){  
       return key;  
    }  
    public int getValue() {  
       return value;  
    }  
 }  
 public class HashMap {  
    private final static int TABLE_SIZE = 128;  
    HashEntry[] table;  
    HashMap() {  
       table = new HashEntry[TABLE_SIZE];  
       for (int i = 0; i < TABLE_SIZE; i++)  
          table[i] = null;  
    }  
    public int get(int key) {  
       int hash = (key % TABLE_SIZE);  
       while (table[hash] != null && table[hash].getKey() != key)  
          hash = (hash + 1) % TABLE_SIZE;  
       if(table[hash] == null)  
          return -1;  
       else  
          return table[hash].getValue();  
    }  
    public void put(int key, int value) {  
       int hash = (key % TABLE_SIZE);  
       while (table[hash] != null && table[hash].getKey() != key)  
          hash = (hash + 1) % TABLE_SIZE;  
       table[hash] = new HashEntry(key, value);  
    }  
 }  
 public class HashTableApp  
 {  
      public static void main(String[]args)  
      {  
      HashMap obj = new HashMap();   
      obj.put(1,10);  
      obj.put(2,18);  
      obj.put(3,15);  
      System.out.println(obj);  
      }  
 }  

Post a Comment

1 Comments

  1. Is this hashMap or HashTable implementation?

    ReplyDelete