Fill in parameters Public New Hash table int initial Capacit

Fill in parameters

Public New Hash table (int initial Capacity, float load Factor) Constructs a new, empty hash table with the specified initial capacity and the specified load factor. Parameters: initial Capacity - the initial capacity of the hash table. load Factor - the load factor of the hash table. Throws: if the initial capacity is less than zero, or if the load factor is nonpositive. public New hostable (int initial 1Capac.ity) Constructs a new, empty hash table with the specified initial capacity and default load factor (0.75). Parameters: initia1Capacity - the initial capacity of the hash table. Throws: IllegalArqumentException - if the initial capacity is less than zero. public NewHashtable() Constructs a new, empty hash table with a default initial capacity (11) and load factor (0.75).

Solution

//NewHashTable.java


import java.util.LinkedList;

@SuppressWarnings({ \"unchecked\", \"unused\", \"rawtypes\", \"hiding\" })
public class NewHashTable<K,V> {
   //private class to hold a key and its value
   private class HashEntry<K,V>{
       private K key;
       private V value;

       public HashEntry(K key, V value){
           this.key = key;
           this.value = value;
       }
       public String toString(){
           return key+\"=\"+value;
       }
       @Override
       public boolean equals(Object obj) {
           if(!(obj instanceof HashEntry))
               return false;
           HashEntry other = (HashEntry) obj;
           return (key.equals(other.key));
       }

   }
   private int capacity;
   private float loadFactor;
   private LinkedList<HashEntry>[] table;
   private int size;

   //Default Constructor
   public NewHashTable(){
       capacity = 11;
       loadFactor = .75f;
       table =new LinkedList[capacity];  
       for(LinkedList<HashEntry> bucket : table)
           bucket = null;
       size = 0;
   }
   //One argument Constructor
   //@param initialCapacity initial capacity of table
   public NewHashTable(int initialCapacity){
       if(initialCapacity<0)
           throw new IllegalArgumentException(\"\"+initialCapacity);
       capacity = initialCapacity;
       loadFactor = .75f;
       table = new LinkedList[capacity];
       for(LinkedList<HashEntry> bucket : table)
           bucket = null;
       size = 0;
   }
   //Two argument Constructor
   //@param initialCapacity initial capacity of this NewHashTable
   //@param loadFactor customized loadFactor
   public NewHashTable(int initialCapacity, float loadFactor){
       if(initialCapacity<0)
           throw new IllegalArgumentException(\"Capacity: \"+initialCapacity);
       if(loadFactor<0)
           throw new IllegalArgumentException(\"Load Factor: \"+loadFactor);
       capacity = initialCapacity;
       this.loadFactor = loadFactor;
   }
   //@return capacity the capacity of this NewHashTable
   public int getCapacity(){
       return capacity;
   }
   //@return true if this NewHashTable is empty, false otherwise
   public boolean isEmpty(){
       return size==0;
   }
   //@return size the number of keys in this NewHashTable
   public int size(){
       return size;
   }
   //@param key key to be searched for in this NewHashTable
   //@return true if key is found within this NewHashTable
   public boolean containsKey(K key){
       int hash = Math.abs(key.hashCode()%capacity);
       return table[hash]!=null
               &&table[hash].contains(new HashEntry<K,V>(key,null));
   }
   //Increases the capacity and reorganizes this NewHashTable
   //Called automatically when size exceeds the capacity and
   //the loadFactor in put() method
   private void rehash(){
       int newCapacity = capacity*2,hash=0,length=0; //doubles capacity
       LinkedList<HashEntry>[] newTable = new LinkedList[newCapacity];
       for(int i = 0; i < capacity;i++){
           if(table[i]!=null){
               length = table[i].size();
               for(int j=0;j<length;j++){
                   HashEntry<K,V> entry = table[i].get(j);
                   hash = Math.abs(entry.key.hashCode()%newCapacity);
                   if(newTable[hash]==null)
                       newTable[hash]=new LinkedList<HashEntry>();
                   newTable[hash].add(entry);
               }
           }
       }  
       table = newTable;
       capacity = newCapacity;
       System.out.println(\"Rehash Complete\");
   }
   //Returns the value to which key is mapped, null if
   //this NewHashTable does not contain this key
   //@param key the key associated with the value to be returned
   //@return the value to which key is mapped
   public V get(K key){
       if(key == null)
           throw new NullPointerException(\"Key: \"+key);
       if(!containsKey(key))
           return null;
       int hash = Math.abs(key.hashCode()%capacity);
       int i = table[hash].indexOf(new HashEntry<K,V>(key,null));
       size++;
       return (V) table[hash].get(i).value;
   }
   //Maps a key to a value in this table
   //@param key NewHashTable key
   //@param value the value to which key will be mapped
   //@return the previous value mapped to key, null if there was
   //no previous mapping
   public V put(K key, V value){
       if(key == null || value == null)
           throw new NullPointerException(\"Key: \"+key+\" Value: \"+value);
       int hash = Math.abs(key.hashCode()%capacity);
       V previousValue = null;
       if(table[hash]==null){
           table[hash] = new LinkedList<HashEntry>();
           table[hash].add(new HashEntry<K,V>(key,value));
       }
       else{
           int i = table[hash].indexOf(new HashEntry<K,V>(key,null));
           if(i!=-1){
               previousValue = (V) table[hash].get(i).value;
               table[hash].set(i,new HashEntry<K,V>(key,value));
           }
           else
               table[hash].add(new HashEntry<K,V>(key,value));
       }
       size++;
       if(size>=loadFactor*capacity)
           rehash();
       return previousValue;  
   }
   //Removes a key and its value from this NewHashTable
   //@param key the key to be removed
   //@return the value key was mapped to, null if key
   //was not mapped to a value
   public V remove(K key){
       if(key == null)
           throw new NullPointerException(\"Key is null\");  
       int hash = Math.abs(key.hashCode()%capacity);
       if(!containsKey(key))
           return null;
       int i = table[hash].indexOf(new HashEntry(key,null));
       size--;
       return (V) table[hash].remove(i).value;
   }
   //clears this NewHashTable
   public void clear(){
       for(LinkedList<HashEntry> bucket : table)
           bucket = null;
       size = 0;
   }
   //@return a String representation of this NewHashTable
   public String toString(){
       if(!isEmpty()){
           String s = \"{\";
           for(int i = 0; i < capacity;i++){
               if(table[i]!=null){
                   for(int j = 0; j < table[i].size();j++)
                       s+=table[i].get(j)+\",\ \";
               }
           }
           return s.substring(0,s.length()-3)+\"}\";
       }
       return \"{}\";
   }
}

Fill in parameters Public New Hash table (int initial Capacity, float load Factor) Constructs a new, empty hash table with the specified initial capacity and th
Fill in parameters Public New Hash table (int initial Capacity, float load Factor) Constructs a new, empty hash table with the specified initial capacity and th
Fill in parameters Public New Hash table (int initial Capacity, float load Factor) Constructs a new, empty hash table with the specified initial capacity and th
Fill in parameters Public New Hash table (int initial Capacity, float load Factor) Constructs a new, empty hash table with the specified initial capacity and th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site