Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1. class SolutionHashTable {
  2.   public Entry[] table;
  3.   public int capacity;
  4.  
  5.   /**
  6.    * Constructs a new HashTable.
  7.    *
  8.    * Capacity of the hash table can not be 0 or negative.
  9.    *
  10.    * @param capacity
  11.    *     to be used as capacity of the table.
  12.    * @throws IllegalArgumentException
  13.    *     if the input capacity is invalid.
  14.    */
  15.   public SolutionHashTable(int capacity) {
  16.     if(capacity <= 0){
  17.       throw new IllegalArgumentException();
  18.     }
  19.     this.table = new Entry[capacity];
  20.     this.capacity = capacity;
  21.   }
  22.  
  23.   /**
  24.    * Add a new Entry to the hash table,
  25.    * uses linear probing to deal with collisions.
  26.    *
  27.    * Returns false, if the key is null or the table is full.
  28.    *
  29.    * @param key
  30.    *     String representing the key of the entry.
  31.    * @param value
  32.    *     String representing the value of the entry.
  33.    * @return true iff entry has been added successfully, else false.
  34.    */
  35.    //might be missing case for when key is already in table.
  36.   public boolean put(String key, String value) {
  37.     if(key == null) return false;
  38.    
  39.     int hashedKey = hash(key);
  40.     int initHashedKey = hash(key);
  41.    
  42.     //case for hasedKey > capacity
  43.     if(hashedKey >= capacity) return false;
  44.    
  45.     //case for no collision
  46.     if(this.table[hashedKey] == null){
  47.       this.table[hashedKey] = new Entry(key, value);
  48.       return true;
  49.     }
  50.    
  51.     //case for collision
  52.     //going from index = hashedKey to end of array.
  53.     while(hashedKey < capacity){
  54.       //if there is a null index (empty), then just insert
  55.       if(this.table[hashedKey] == null){
  56.         this.table[hashedKey] = new Entry(key, value);
  57.         return true;
  58.       }
  59.       //if there is a defunct, then just insert
  60.       if(this.table[hashedKey].getKey() == null){
  61.         this.table[hashedKey] = new Entry(key, value);
  62.         return true;
  63.       }
  64.       //if there is an entry with the same key, replace the entry
  65.       if(this.table[hashedKey].getKey().equals(key)){
  66.         this.table[hashedKey] = new Entry(key, value);
  67.         return true;
  68.       }
  69.       hashedKey++;
  70.     }
  71.    
  72.     //going from index = 0 to initial hashedKey
  73.     hashedKey = 0;
  74.     while(hashedKey <= initHashedKey){
  75.       //if there is a null index (empty), then just insert
  76.       if(this.table[hashedKey] == null){
  77.         this.table[hashedKey] = new Entry(key, value);
  78.         return true;
  79.       }
  80.       //if there is a defunct, then just insert
  81.       if(this.table[hashedKey].getKey() == null){
  82.         this.table[hashedKey] = new Entry(key, value);
  83.         return true;
  84.       }
  85.       //if there is an entry with the same key, replace the entry
  86.       if(this.table[hashedKey].getKey().equals(key)){
  87.         this.table[hashedKey] = new Entry(key, value);
  88.         return true;
  89.       }
  90.       hashedKey++;
  91.     }
  92.    
  93.     //Then the array must be full
  94.     return false;
  95.   }
  96.  
  97.   /**
  98.    * Retrieve the value of the entry associated with this key.
  99.    *
  100.    * Returns null, if the key is null.
  101.    *
  102.    * @param key
  103.    *     String representing the key of the entry to look for.
  104.    * @return value of the entry as String iff the entry with this key is found in the hash table, else null.
  105.    */
  106.   public String get(String key) {
  107.     if(key == null) return null;
  108.    
  109.     int hashedKey = hash(key);
  110.     int initHashedKey = hash(key);
  111.     // checking from index = hashedKey to end of array
  112.     while(hashedKey < capacity){
  113.       if(this.table[hashedKey] == null){
  114.         return null;
  115.       }
  116.       // if index is not null, check if the key of that element in index is same as key
  117.       if(this.table[hashedKey].getKey() != null) {
  118.         Entry e = this.table[hashedKey];
  119.         if(e.getKey() == null) return null;
  120.         if(e.getKey().equals(key)){
  121.           return e.getValue();
  122.         }
  123.       }
  124.       // if we find a null index, that means the key is not in the table
  125.       else {
  126.         return null;
  127.       }
  128.       hashedKey++;
  129.     }
  130.    
  131.    
  132.     // checking from index = 0 to index = hashedKey
  133.     hashedKey = 0;
  134.       while(hashedKey <= initHashedKey){
  135.       if(this.table[hashedKey] == null){
  136.         return null;
  137.       }
  138.       // if index is not null, check if the key of that element in index is same as key
  139.       if(this.table[hashedKey].getKey() != null) {
  140.         Entry e = this.table[hashedKey];
  141.         if(e.getKey() == null) return null;
  142.         if(e.getKey().equals(key)){
  143.           return e.getValue();
  144.         }
  145.       }
  146.       // if we find a null index, that means the key is not in the table
  147.       else {
  148.         return null;
  149.       }
  150.       hashedKey++;
  151.     }
  152.     return null;
  153.   }
  154.  
  155.   /**
  156.    * Remove the entry associated with this key from the hash table.
  157.    *
  158.    * Returns false, if the key is null.
  159.    *
  160.    * @param key
  161.    *     String representing the key of the entry to remove.
  162.    * @return true iff the entry has been successfully removed, else false.
  163.    */
  164.   public boolean remove(String key) {
  165.     if(key == null) return false;
  166.    
  167.     // case : key is not in the table.
  168.     if(get(key) == null) return false;
  169.    
  170.     int hashedKey = hash(key);
  171.     int initHashedKey = hash(key);
  172.    
  173.     while(hashedKey < capacity){
  174.       // if index is not null, check if the key of that element in index is same as key
  175.       if(this.table[hashedKey] != null) {
  176.         Entry e = this.table[hashedKey];
  177.         if(e.getKey() == null) return false;
  178.         if(e.getKey().equals(key)){
  179.           setDefunct(hashedKey);
  180.           return true;
  181.         }
  182.         hashedKey++;
  183.       }
  184.     }
  185.    
  186.    
  187.     // checking from index = 0 to index = hashedKey
  188.     hashedKey = 0;
  189.       while(hashedKey <= initHashedKey){
  190.       // if index is not null, check if the key of that element in index is same as key
  191.       if(this.table[hashedKey] != null) {
  192.         Entry e = this.table[hashedKey];
  193.         if(e.getKey() == null) return false;
  194.         if(e.getKey().equals(key)){
  195.           setDefunct(hashedKey);
  196.           return true;
  197.         }
  198.         hashedKey++;
  199.       }
  200.     }
  201.     return false;
  202.   }
  203.  
  204.   /**
  205.    * Takes as input an index and sets the entry in that location as defunct.
  206.    *
  207.    * @param index
  208.    *     The index of the spot that is defunct.
  209.    */
  210.   public void setDefunct(int index) {
  211.     this.table[index] = new Entry(null, null);
  212.   }
  213.  
  214.   /**
  215.    * Hashes a string representing a key.
  216.    *
  217.    * @param key
  218.    *     String that needs to be hashed.
  219.    * @return the hashcode of the string, modulo the capacity of the HashTable.
  220.    */
  221.   public int hash(String key) {
  222.     return Math.abs(key.hashCode()) % capacity;
  223.   }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement