2016-11-05 32 views
-4

我必須比較Java中的兩個類似表,可能使用集合類。但輸入源是剪貼板而不是數據庫。樣本表: -比較Java中的兩個表(不是數據庫)

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| 

這裏僱員ID是唯一的。 Table1的Row1應與Table2的Row1比較&等。如果EmpID &中的相應列中的數據匹配,則應顯示爲匹配。 如果EmpID與第二行不匹配,則應報告爲差異,搜索表2的下一行&找到EmpID。報告是否匹配。 如果未找到,則應在Table1中報告額外報告或在Table2中報告Miss。所有這些行必須在比較爲Match,Diff,Extra或Miss之後進行報告。

試圖在此處使用嵌套的HashMap進行比較,當只比較一行時,它可以正常工作。 當多個行添加到HashMap中時,它執行笛卡爾產品比較I.e.表1,行1與表2,行2 &等因此得到錯誤的輸入。 (請參閱下面的示例代碼)。 能否請您建議我如何做這些比較/迭代,因爲我還必須在比較之後進行報告。

HashMap<String,String> Table1= new HashMap<String,String>(); 
Table1. put(Name, "John"); 
Table1. put(Age,"30"); 
Table1.put(Dept, "Ops"); 

HashMap<String,String> Table2= new HashMap<String,String>(); 
Table2.put(Name, "John"); 
Table2. put(Age,"30"); 
Table2.put(Dept, "Ops"); 

HashMap<String, HashMap<String, String>>Table3= new HashMap<String, HashMap<String,String>>(); 
Table3.put(EmpId, "123"); 

HashMap<String, HashMap<String, String>>Table4= new HashMap<String, HashMap<String,String>>(); 
Table4.put(EmpId, "123"); 

List<String> match = new List<String>(); 
List< String> diff = new List<String>(); 

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"); 
} 

所需的輸出是:

匹配度: 的Emp ID:123 鍵:名字,年齡,DEPT 值:約翰,30,行動

DIFF: 的Emp ID:456 重點:部門 值:人力資源,開發

附加: InTable1:的Emp ID:789(打印鍵&的值,如果有的話) 在表2:EMPID:879(打印中的任意鍵&值)

小姐: 在表1: 的Emp ID:489 名字,年齡,DEPT 的Xyz,26,行動 在表2:同樣如果有的話

+0

你可以發佈你的例子所需的輸出嗎?要求對我來說不是很清楚 – Massimo

+0

您應該使用類/對象來適當保存您的數據,當您發現自己嵌套地圖/集合時,幾乎總是有時間重新考慮設計(正如您所看到的,這會變得非常複雜和混亂)快速)。然後比較將是關於一行代碼 – Rogue

回答

1

Shardha讓我們做出更好的設計方法。

在這裏,我已經作爲2個hashMap預備了2個表。每個hashmap的key是empid,value是一個「EmpDetails」對象。這empdetails將年齡,名稱,部門作爲屬性。我們可以使用構造函數設置這些值。在這裏,我也重寫了「equals」方法來比較2個對象。

我還沒有正確理解你對「小姐」和「額外」的要求。但我認爲它是一個小細分市場,你可以通過它自己添加它。請找主類如下:

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 


    public class Main { 
    public static void main(String[] args) { 
    Map<String,EmpDetails> table1=new HashMap<String,EmpDetails>(); 
    Map<String,EmpDetails> table2=new HashMap<String,EmpDetails>(); 

    EmpDetails emp1=new EmpDetails("John", "30", "Ops"); 
    table1.put("123",emp1); 

    EmpDetails emp2=new EmpDetails("John", "30", "Ops"); 
    table2.put("123",emp2); 

    EmpDetails emp3=new EmpDetails("Amy", "30", "Ops"); 
    table1.put("127",emp3); 

    EmpDetails emp4=new EmpDetails("Rachel", "30", "Ops"); 
    table2.put("127",emp4); 

    List<String> match = new ArrayList<String>(); 
    List< String> diff = new ArrayList<String>(); 

    for(String empIdTable1:table1.keySet()) 
    { 
     EmpDetails tempemp1; 
     EmpDetails tempemp2; 
     for(String empIdTable2:table2.keySet()) 
     { 
      if(empIdTable1.equalsIgnoreCase(empIdTable2)) 
      { 
       tempemp1=table1.get(empIdTable1); 
       tempemp2=table2.get(empIdTable2); 
       if(tempemp1.equals(tempemp2)) 
       { 
        match.add(empIdTable1); 
        break; 
       }else{ 
        diff.add(empIdTable1); 
       } 
      } 
     } 

    } 
    System.out.println(" Match :"+match); 
    System.out.println(" diffn :"+diff); 

} 

} 

這裏是Empdeatils類:

public class EmpDetails { 
    private String name; 
    private String age; 
    private String dept; 
    public EmpDetails(String name, String age, String dept) { 
    super(); 
    this.name = name; 
    this.age = age; 
    this.dept = dept; 
    } 

@Override 
public boolean equals(Object obj) { 
    // TODO Auto-generated method stub 
    Boolean response=false; 
    if(obj!=null && obj instanceof EmpDetails) 
    { 
     EmpDetails objParam=(EmpDetails)obj; 

     if(objParam.name.equalsIgnoreCase(this.name) && objParam.age.equalsIgnoreCase(this.age) && objParam.dept.equalsIgnoreCase(this.dept)) 
     { 
      response=true; 
     } 
    } 
    return response; 
    } 


    } 

請註明您的進一步查詢,在評論這個答案。