title: How to add and compare keys of a generic hash table in Java?
body: <p>I'm writing my own class for a generic hash table and trying to create an adding method as well as a method for comparing keys and returning true or false if they exist. So far my add method is not adding anything into my buckets, and my in my main, I cant pass any data type into my generic compare method without it asking me to define a new method with that type. Could anyone help point out the issues with my code as I'm trying to learn how to implement the data structure? Thank you.</p>
  <p>My node and my table classes</p>
  <pre><code>import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Objects;
  
  class GenericHashNode &lt;K, V&gt; {
      
      K key;
      V value;
      
      GenericHashNode&lt;K, V&gt; next;
      
      public GenericHashNode (K key, V value) {
          this.key = key;
          this.value = value;
          next = null;
      }
      
      public GenericHashNode() {
          next = null;
      }
      
  }
  
  
  
  public class GenericHashTable &lt;K, V&gt; {
  
      private ArrayList&lt;GenericHashNode&lt;K, V&gt; &gt; hashArray;
      private int tableSize;
  
      
      public GenericHashTable(int tableSize)
      {
          hashArray = new ArrayList&lt;&gt;(tableSize);
          this.tableSize = tableSize;
   
          for (int i = 0; i &lt; tableSize; i++) {
              hashArray.add(new GenericHashNode());
          }          
      }
      
      private int getTableSize() {
          return tableSize;
      }
      
      private int createHashCode(K key)
      {
          int h = key.hashCode();
          h = h % getTableSize();
          int hashCode = h &lt; 0 ? h -= -h : h;
          
          return hashCode;
      }
      
      public void add(K key) {
          
          int hashIndex = createHashCode(key);
          
          GenericHashNode head = hashArray.get(hashIndex);
          GenericHashNode newNode = new GenericHashNode(key, key);
          
          newNode.next = head.next;
          head.next = newNode;
          
      }
      
      public boolean compare(K key) {
          
          int hashIndex = createHashCode(key);
          GenericHashNode current = new GenericHashNode();
          current = hashArray.get(0);
          
          Iterator&lt;GenericHashNode&lt;K, V&gt;&gt; iterate = hashArray.iterator(); 
          
          while(iterate.hasNext()) {
              if(current == null) {
                  current = current.next;
              }
              else {
                  if(current.key == key) {
                      return true;
                  }
                  else {
                      current = current.next;
                  }
              }
              
              
          }
          return false;
          
      }
      
      
      
  }
  </code></pre>