fork(2) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception {
  7.  
  8. Map<String,Object> oldMap = new HashMap<String,Object>();
  9. Map<String,Object> newMap = new HashMap<String,Object>();
  10.  
  11. oldMap.put("initials","Y. J.");
  12. oldMap.put("firstName","Yogend");
  13. oldMap.put("lastName","Jos");
  14. oldMap.put("userName","YNJos");
  15. oldMap.put("age","33");
  16.  
  17. newMap.put("initials","Y. J.");
  18. newMap.put("firstName","Yogendra");
  19. newMap.put("lastName","Joshi");
  20. newMap.put("userName","YNJoshi");
  21. newMap.put("age","33");
  22.  
  23. Map<String,Boolean> diffMap = Main.scanForDifferences(oldMap, newMap);
  24.  
  25. Iterator<Map.Entry<String, Boolean>> it = diffMap.entrySet().iterator();
  26. while (it.hasNext()) {
  27. Map.Entry<String,Boolean> entry = (Map.Entry<String,Boolean>)it.next();
  28. System.out.println("Field [" + entry.getKey() +"] is " +(entry.getValue()?"NOT ":"") + "different" );
  29. }
  30.  
  31. }
  32.  
  33. private static Map<String,Boolean> scanForDifferences(Map<String,Object> mapOne, Map<String,Object> mapTwo){
  34. Map<String,Boolean> retMap = new HashMap<String,Boolean>();
  35.  
  36. Iterator<Map.Entry<String, Object>> it = mapOne.entrySet().iterator();
  37. while (it.hasNext()) {
  38. Map.Entry<String,Object> entry = (Map.Entry<String,Object>)it.next();
  39. if (mapTwo.get(entry.getKey()).equals(entry.getValue()))
  40. retMap.put(entry.getKey(), new Boolean(Boolean.TRUE));
  41. else
  42. retMap.put(entry.getKey(), new Boolean(Boolean.FALSE));
  43. it.remove(); // prevent ConcurrentModificationException
  44. }
  45. return retMap;
  46. }
  47. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
Field [lastName] is different
Field [initials] is NOT different
Field [age] is NOT different
Field [userName] is different
Field [firstName] is different