Search code examples
javahashmapresultset

Compare two data set(two result set) from different data base in java


I am trying to compare two data set from different DB through Java. Source db:-oracle,sql,other database. Target db:Hive

I have made a JDBC connection to both databases and fetched the result in two different result sets.

sourceData  
targetData

Now I want to compare the two result sets and point out the differences. The approach I am thinking is to add both result sets to hash map and compare. Please let me know if my approach is correct or is there a better way to do the same.

Please share links that could help me.

Thanks in advance.


Solution

  • I will help you with hashmap approach itself. Assuming that you set contains unique values. My following code will help you.

    Set<Data> soruceData;
    Set<Data> targetData;
    HashMap<Data,Integer> comparisonMap;
    for(Data data:targetData){
     comparisonMap.put(data,0);
    }
    for(Data data:soruceData){
    if(targetData.contains(data))
     comparisonMap.put(data,1);
    else
     comparisonMap.put(data,0);
    }
    

    Now you got a hashmap which say the difference between two list. 1- means present in two sets 0- mean not present in any one of sets

    NOTE please implement hascode and equals properly while using set and hascode