title: How to write a put method for a HashMap in Java?
body: <p>I've been writing a put method for my HashMap code and I cannot seem to understand what is the problem. </p>
  
  <p>My instructions read as:</p>
  
  <p>If the given key is already present in the HashMap, the old value should be replaced with the new value. If a collision occurs, linear probing should be used to find the next "open" array index. If an additional element will be added to the map and the load factor is greater than or equal to the MAX_LOAD_FACTOR, then the map should be rehashed before identifying the bucket to try to store the additional element.</p>
  
  <p>Therefore, I'm not sure if it's my put method that isn't working properly, or my rehash one. Therefore, I am going to post both of them. Hopefully it makes sense.</p>
  
  <pre><code>public class HashMap&lt;K,V&gt;
  {
  private final double MAX_LOAD_FACTOR = 0.75;
  private HashEntry[] elementData;
  private final HashEntry REMOVED = new HashEntry(null, null);
  private int size;
  
  public HashMap()
  {
      this.elementData = new HashMap.HashEntry[10];
      size = 0;
  }
  public void put(K key, V value)
  {
  int hash = hashFunction(key);
      if (!containsKey(key))
      {
          if(loadFactor() &gt;= MAX_LOAD_FACTOR)
          {
              rehash();
          }
          while(elementData[hash] != null)
          {
              hash = (hash + 1) % elementData.length;
          }
          elementData[hash] = new HashEntry(key, value);
          size++;
      }
      else
      {
          while(elementData[hash] != null || (elementData[hash] != REMOVED &amp;&amp; !elementData[hash].getKey().equals(key)))
          {
              hash = (hash + 1) % elementData.length;
          }
              elementData[hash].value = value;
      }
  }
   private int hashFunction(Object key)
  {
      return Math.abs(key.hashCode()) % elementData.length;
  }
  private double loadFactor()
  {
      return (double) size / elementData.length;
  }
  private void rehash()
  {
      HashEntry[] oldElementData = elementData;
      elementData = new HashMap.HashEntry[2 * oldElementData.length];
      size = 0;
      for (int i = 0; i &lt; oldElementData.length; i++)
      {
          HashEntry current = oldElementData[i];
          if(current != null)
          {
              put(current.getKey(), current.getValue());
          }
      }
  }
   public class HashEntry
  {
      private K key;
      private V value;
  
      public  HashEntry(K key, V value)
      {
          this.key = key;
          this.value = value;
      }
      public K getKey()
      {
          return key;
      }
      public V getValue()
      {
          return value;
      }
   }
  }
  </code></pre>
  
  <p>drop down any ideas that will help out. Thanks. </p>