title: Map with two-dimensional key in java
body: <p>I want a map indexed by two keys (a map in which you put AND retrieve values using two keys) in Java. Just to be clear, I'm looking for the following behavior: </p>
  
  <pre><code>map.put(key1, key2, value); 
  map.get(key1, key2); // returns value
  map.get(key2, key1); // returns null
  map.get(key1, key1); // returns null
  </code></pre>
  
  <p>What's the best way to to it? More specifically, should I use:</p>
  
  <ul>
  <li><p><code>Map&lt;K1,Map&lt;K2,V&gt;&gt;</code></p></li>
  <li><p><code>Map&lt;Pair&lt;K1,K2&gt;, V&gt;</code></p></li>
  <li><p>Other?</p></li>
  </ul>
  
  <p>(where K1,K2,V are the types of first key, second key and value respectively)</p>