title: Comparing two tables in Java (not database)
body: <p>I have to compare 2 similar tables in Java, probably using collection class. But the input source is clipboard not database. Sample tables:-</p>
  
  <pre><code>Table1:
   |EmpID| Name| Age|Dept|
   |123|John|30|Ops|
   |456|Peter|40|HR|
  
  Table2:
  |EmpID| Name| Age|Dept|
  |123|John|30|Ops|
  |462|Peter|40|Dev|
  </code></pre>
  
  <p>Here Emp ID is unique. Row1 of Table1 should compare to Row1 of Table2 &amp; so on. If EmpID &amp; data in corresponding columns match it should display as match.
  If EmpID do not match as in 2nd row it should report as  Diff, Search in next row of Table2 &amp; find the EmpID. Report if match.
  If not found should report as extra in Table1 or Miss in Table2. All These rows have to be reported after comparison as Match, Diff,Extra or Miss.</p>
  
  <p>Have tried to use nested HashMap here for comparison purpose which works fine when only one row is to be compared.
  When multiple rows are added to the HashMap, it does a Cartesian product comparison I.e. Table1, Row 1 with Table2 ,Row2 &amp; so on hence getting wrong input. (Please see a sample code below). 
  Could you please suggest how I could do these comparisons/iterations as I also have to do reporting after the comparison.</p>
  
  <pre><code>HashMap&lt;String,String&gt; Table1= new HashMap&lt;String,String&gt;();
  Table1. put( Name, "John");
  Table1. put(Age,"30");  
  Table1.put(Dept, "Ops"); 
  
  HashMap&lt;String,String&gt; Table2= new   HashMap&lt;String,String&gt;();
  Table2.put( Name, "John");
  Table2. put(Age,"30"); 
  Table2.put(Dept, "Ops"); 
  
  HashMap&lt;String, HashMap&lt;String, String&gt;&gt;Table3= new    HashMap&lt;String, HashMap&lt;String,String&gt;&gt;();
  Table3.put(EmpId, "123"); 
  
  HashMap&lt;String, HashMap&lt;String, String&gt;&gt;Table4= new   HashMap&lt;String, HashMap&lt;String,String&gt;&gt;();
  Table4.put(EmpId, "123"); 
  
  List&lt;String&gt; match = new List&lt;String&gt;();
  List&lt; String&gt; diff = new List&lt;String&gt;();
  
  for ( String tempe: Table3.keySet()){ 
  for(String temp: Table4.keySet()) {
  
  If(tempe.equals temp){
  System.out.println("Emp Id match");
  
  for(String tem: Table1.keySet()){
  for(String te: Table2.keySet()){
  
  If(tem equals te){
  match.add( tem);
  }else{
  diff.add(temp + te);
  } 
  }
  }
  else{
  System.out.println(" EmpId not matching");
  }
  }
  }
  
  for( String mat: match){
   System.out println("Matched values: + mat ");
   }
  for( String dif: diff){
  System.out.println("Different values: + dif");
  }
  </code></pre>
  
  <p>The desired output is:</p>
  
  <p>Match:
  Emp ID : 123 
  Key:  Name, Age, Dept
  Values: John, 30, Ops</p>
  
  <p>Diff:
  Emp ID: 456
  Key: Dept
  Values: HR, Dev</p>
  
  <p>Extra :
  InTable1 : Emp ID: 789 (print key &amp; values, if any) 
  In Table2:  EmpId : 879 (print key &amp; values of any)</p>
  
  <p>Miss:
   In Table1: 
  Emp ID: 489
  Name, Age, Dept
  Xyz, 26, Ops
  In Table2: similarly if any</p>