title: Map with multiple keys
body: <p>I am trying to implement a map like </p>
  
  <pre><code>Map&lt;&lt;key1, key2&gt;, List&lt;value&gt;&gt;
  </code></pre>
  
  <p>Map should contain 2 keys and corresponding value would be a list. I want to add records in same list <strong>if alteast one key value is equal</strong>
  For example consider following records    </p>
  
  <pre><code>R1[key1, key2]
  R2[key1, null/empty] - Key1 is equal
  R3[null/empty, key2] - Key2 is equal
  R4[key1, key2] - Key1 and Key2 both are equal.
  </code></pre>
  
  <p>all should be inserted in same list like </p>
  
  <pre><code>Key = &lt;Key1,Key2&gt; 
  Value = &lt;R1, R2, R3, R4&gt;
  </code></pre>
  
  <p>I cant use <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html" rel="nofollow">Guava table</a> or <a href="http://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/map/MultiKeyMap.html" rel="nofollow">commons MulitKeyMap</a> (dont want include whole library just for this) .    </p>
  
  <p>I tried to implement a class (which I can use as a key) which will have both <code>key1</code> and <code>key2</code> as attribute but implementing a effective hashcode which don't consider key1 and key2 seems bit (or may be a lot) tricky</p>
  
  <pre><code>public class Key {
      private int key1;
      private int key2;
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          // Cant include key1 and key2 in hashcode 
         /* result = prime * result + key1;
          result = prime * result + key2;*/
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Key other = (Key) obj;
          if(key2 and other.key2 both not blank/null){ // pseudo code
          if (key2 == other.key2)
              return true;
          }
          if(key1 and other.key1 both not blank/null){ //pseudo code
          if (key1 == other.key1)
              return true;
          }
          return true;
      }
  
  }
  </code></pre>
  
  <p>It will work if I use the same hashcode for all but it will impact the performance as I have thousands of records.</p>
  
  <hr>
  
  <p><strong>EDIT :</strong><br>
  <strong>I cant use nested Maps</strong> like  </p>
  
  <pre><code>Map&lt;key1, Map&lt; key2, List&lt;value&gt;&gt;&gt;
  </code></pre>
  
  <p>As some records might have only one key.</p>
  
  <pre><code>  R1[key1, key2]     - Have both keys
    R2[key1, null/empty] - Key1 is equal
    R3[null/empty, key2] - Key1 is missing and key2 is equal
  </code></pre>
  
  <p>Here R3 dont have key1 and hence cant be inserted in same location as R1 and R2</p>
  
  <hr>
  
  <p><strong>EDIT 2 :</strong> </p>
  
  <p>I also wish to maintain the intertion order.</p>