title: Simple HashTable implementation using an array in Java?
body: <p>I'm having a problem with implementing a very simple HashTable using an array. The problem is that the first Item put in the <code>HashTable</code> is always <code>AVAILABLE</code>. Maybe you guys can see what is going wrong. This is the <code>HashTable</code> class:</p>
  
  <pre><code>public class HashTable {
  
      private Item[] data;
      private int capacity;
      private int size;
      private static final Item AVAILABLE = new Item("Available", null);
  
      public HashTable(int capacity) {
  
          this.capacity = capacity; 
          data = new Item[capacity];
          for(int i = 0; i &lt; data.length; i++) {
  
              data[i] = AVAILABLE;
          }
          size = 0;
      }
  
      public int size() {
  
          return size;
      }
  
      public int hashThis(String key) {
  
          return key.hashCode() % capacity; 
      }
  
      public Object get(String key) {
  
          int hash = hashThis(key);
  
          while(data[hash] != AVAILABLE &amp;&amp; data[hash].key() != key) {
  
              hash = (hash + 1) % capacity;
          }
          return data[hash].element();
      }
  
      public void put(String key, Object element) {
  
          if(key != null) {
              size++;
              int hash = hashThis(key);
              while(data[hash] != AVAILABLE &amp;&amp; data[hash].key() != key) {
  
                  hash = (hash + 1) % capacity;
              }
  
              data[hash] = new Item(key, element);
  
          }
  
      }
  
      public Object remove(String key) {
          // not important now.
          throw new UnsupportedOperationException("Can't remove");
      }
  
      public String toString() {
  
          String s = "&lt;HashTable[";
          for(int i = 0; i &lt; this.size(); i++) {
  
              s += data[i].toString();
              if(i &lt; this.size() - 1) {
  
                  s += ",";
              }
          }
          s += "]&gt;";
          return s;
      }
  
  }
  </code></pre>
  
  <p>For more clarity, this is the <code>Item</code> class:</p>
  
  <pre><code>public class Item {
  
      private String key;
      private Object element;
  
      public Item(String key, Object element) {
  
          this.setKey(key);
          this.setElement(element);
      }
  
      public String key() {
          return key;
      }
  
      public void setKey(String key) {
          this.key = key;
      }
  
      public Object element() {
          return element;
      }
  
      public void setElement(Object element) {
          this.element = element;
      }
  
      public String toString() {
  
          String s = "&lt;Item(";
          s += this.key() + "," + this.element() + ")&gt;";
          return s;
      }
  
  }
  </code></pre>
  
  <p>To give an example: </p>
  
  <pre><code>HashTable ht = new HashTable(10);
  ht.put("1", "a");
  </code></pre>
  
  <p>The output of toString() after putting has to be: </p>
  
  <pre><code>"&lt;HashTable[&lt;Item(1,a)&gt;]&gt;"
  </code></pre>
  
  <p>but I get: </p>
  
  <pre><code>"&lt;HashTable[&lt;Item(Available,null)&gt;]&gt;"
  </code></pre>
  
  <p><strong>update:</strong> I should probably mention that the next Item gets put correctly and the one after that is not again.</p>