title: How to get intersection by a value in java for a list of Objects?
body: <p>Structure of the class:</p>
  
  <pre><code>class MyObject{
  private String key;
  private int value;
  private int num;
  }
  </code></pre>
  
  <p>Creating a list of objects:</p>
  
  <pre><code>List&lt;MyObject&gt; a = new ArrayList&lt;MyObject&gt;();
  </code></pre>
  
  <p>Contents of the list of objects:</p>
  
  <pre><code>"Einstein",12,1
  "Princeton",12,4
  "Einstein",16,3
  "Princeton",16,7
  "Einstein",19,6
  "Princeton",22,6
  "Quantum",12,3
  "Quantum",16,6
  </code></pre>
  
  <p>For Input: <code>"Einstein","Princeton","Quantum"</code></p>
  
  <p>Checking if a key is present in all value fields, if so add up the num field. In this case Einstein, Princeton, Quantum is present only in value 12. So adding up the num field will give 8. Hence,</p>
  
  <p>Expected output list:</p>
  
  <pre><code>12,8   
  </code></pre>
  
  <p>Basically I am trying to get the intersection of value field of the object and summation of the corresponding num field. How to achieve this?</p>
  
  <p>EDIT:
  List xy = Arrays.asList(terms); // terms are the input</p>
  
  <pre><code>    Map&lt;Integer, Integer&gt; check = new HashMap&lt;Integer, Integer&gt;();
      for (int i = 0; i &lt; a.size(); i++) {
  
              if (xy.contains(a.get(i).getKey())) {
  
                  Integer sum = check.get(a.get(i).getNum());
                  if (sum == null)
                      sum = 0;
                  sum += a.get(i).getNum();
                  check.put(a.get(i).getValue(), sum);
              }
  
      }
  </code></pre>
  
  <p>List Contents:</p>
  
  <pre><code>Key: british Value: 899816511 Occ: 8
  Key: naren Value: 899816511 Occ: 1
  Key: einstein Value: 899816511 Occ: 1
  Key: british Value: 562115287 Occ: 1
  Key: einstein Value: 2056958632 Occ: 1
  Key: british Value: 2056958632 Occ: 1
  Key: einstein Value: 1426519040 Occ: 1
  Key: british Value: 1426519040 Occ: 5
  </code></pre>
  
  <p>Input: <code>"british","naren","einstein"</code></p>
  
  <p>Output:</p>
  
  <pre><code>{1426519040=5, 562115287=1, 2056958632=1, 899816511=1}
  </code></pre>