title: Hashmap and how this works behind the scene
body: <p>quick question to be sure I understood well how HashMap in java works.</p>
  
  <p>Here is an example of code:</p>
  
  <pre><code>//String key = new String("key");
  //String val = new String("value");
  String key = "key";
  String val = "value";
  
  HashMap map = new HashMap();
  map.put(key, val);
  
  System.out.println("hashmap object created. Its key hashcode = "+key.hashCode());
  // the hashcode is 106079
  System.out.println("hashmap object value for key = "+map.get(key));
  
  // Let's store using a key with same hashcode
  Integer intkey = new Integer(106079);
  val = "value2";
  map.put(intkey, val);
  System.out.println("hashmap object created. Its intkey hashcode = "+intkey.hashCode());
  // this returns 106079 once again. So both key and intkey have the same hashcode
  
  // Let's get the values
  System.out.println("hashmap object value for intkey = "+map.get(intkey));
  System.out.println("hashmap object value for key = "+map.get(key));
  </code></pre>
  
  <p>the returned values are as expected. I read that behind the scene, HashMap works as follows:</p>
  
  <ol>
  <li>get the key/value.</li>
  <li>make a hashcode from the key.</li>
  <li>store in bucket the key and value objects (in my case bucket number 106079)</li>
  </ol>
  
  <p>same again for the second one.</p>
  
  <ol start="4">
  <li>stores it within the same bucket but as this is at this point a LinkedList, I suppose store it at "next available allocation".</li>
  </ol>
  
  <p>To get it:</p>
  
  <ol>
  <li>pick up the key, get hashcode,</li>
  <li>look at the bucket,</li>
  <li>then look at the key from the first element in the LinkedList,</li>
  <li>then check if key passed and key from element match, if not then continue to the next, and so on until value can be retrieved.</li>
  </ol>
  
  <p>Am I understanding this concept correctly?</p>
  
  <p>Many thanks!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>many thanks, so to complete:
   - <a href="https://stackoverflow.com/questions/9603024/how-does-java-hashmap-store-entries-internally">How does Java HashMap store entries internally</a>
   - <a href="https://stackoverflow.com/questions/6493605/how-does-a-hashmap-work-in-java">How does a Java HashMap handle different objects with the same hash code?</a></p>
  
  <p>And:</p>
  
  <ul>
  <li>There shouldn't be that many "buckets"</li>
  <li>A bucket is not the same as an entry. A bucket is a logical collection of all entries sharing the same bucket# (which in the Java case is a function of the key's hashCode()). As stated in other answers, bucket overflow could be implemented several ways, not necessarily in a List.</li>
  <li>There are other existing collision resolutions: <a href="http://en.wikipedia.org/wiki/Hash_table#Collision_resolution" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Hash_table#Collision_resolution</a></li>
  <li>it uses Object's equals method to compare and retrieve objects from the same bucket.</li>
  </ul>