Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not totally sure I understand your question.</p> <p>In a standard <a href="http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html" rel="nofollow">HashMap</a>, if you put another value with the same key, it replaces the first one.</p> <p>But you may use for example a <a href="http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiValueMap.html" rel="nofollow">MultiMap</a> (from Apache commons) to store more than one value per key.</p> <p>If your question is related to more than one key having the same index, this is handled by the implementation : the keys for are stored in linked lists (one for each bucket) and real comparisons (using <code>equals</code>) are done on get and put operations so that no collision can happen as long as <code>equals</code> returns false.</p> <p>As with any collection related problem, the important thing to do if you intend to use your own class is to correctly implement the <code>equals</code> and <code>hashcode</code> methods (read <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#equals%28java.lang.Object%29" rel="nofollow">this</a>).</p> <p><strong>Regarding your question in edit :</strong></p> <p>This is a feature of HashMap that the first value with a given key is erased by the new one. So "Mexico" is removed from the HashMap when you add "New Mexico". </p> <p>If you want to have more than one value for a given key, use a MultiMap (or simply use a <code>HashMap&lt;Integer, List&lt;String&gt;&gt;</code> but the <code>put</code> operation is a little more tedious).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload