-3

I will explain the scenario, I have two hash maps lets take

Map<String,String> map_A = new HashMap<>(); 

mapA.put("Name", "Jerry");
mapA.put("State", "Texas");

Map<String,String> map_B = new HashMap<>();

mapB.put("Name","TOM");
mapB.put("State", "Texas");

My keys will be same in all cases.

Expected output :

Name Jerry TOM mismatched

I need to print only in case if there is a mismatch.

Prishi Kumar
  • 81
  • 1
  • 2
  • 9
  • Do you have a question? If you have tried this and are having trouble getting it to work, please post what you tried. If you haven't tried it, please try first. – ajb Nov 11 '17 at 06:54
  • 1
    Downvoted for lack of attempt / prior research. This is really basic stuff, and there are zillions of examples for such things. Beyond that : read about raw types - and then stop using them. You should put type information on the left hand side of your map declarations! – GhostCat Nov 11 '17 at 06:54
  • what's the problem ? Get the values on key basis compare the values if not equal then print the keys. – Lokesh Pandey Nov 11 '17 at 07:01
  • @Lokesh I need to compare both the maps ... keys will be same if the values are mismatching i need to display in console – Prishi Kumar Nov 11 '17 at 07:12
  • @PrishiKumar check the answer – Lokesh Pandey Nov 11 '17 at 07:16
  • @PrishiKumar I have updated the answer according to your needs – Lokesh Pandey Nov 11 '17 at 07:48

2 Answers2

0

You can try this:

public static void main(String[] args) {
    Map<String,String> mapA = new HashMap<>();
    mapA.put("Name", "Jerry");
    mapA.put("State", "Texas");
    Map<String,String> mapB = new HashMap<>();
    mapB.put("Name","TOM");
    mapB.put("State", "Texas");
    Set<String> bKeys = mapB.keySet();
    mapA.keySet().stream()
            .filter(bKeys::contains)
            .distinct()
            .map(key -> {
                String valA = mapA.get(key);
                String valB = mapB.get(key);
                if (Objects.equals(valA, valB)) {
                    return null;
                }
                return key + " " + valA + " vs " + valB + " mismatched";
            })
            .filter(Objects::nonNull)
            .forEach(System.out::println);
}
Yegor Babarykin
  • 705
  • 3
  • 12
  • @PrishiKumar read about streams https://www.tutorialspoint.com/java8/java8_streams.htm – Yegor Babarykin Nov 11 '17 at 07:16
  • Thanks yegor, but this checks only the first pair – Prishi Kumar Nov 11 '17 at 07:36
  • No, it's itarate for all keys of mapA and MapB and gets distinct key values. After that we checks that values in map are not equals and creates the string with diff – Yegor Babarykin Nov 11 '17 at 07:53
  • The problem is when I use this method, map 1 data is coming correctly from database but for map2 values I am getting values which are not in database. when I tried displaying the key and value is coming correctly, few values are coming as null for map 2.. and how I can call this from main method ? – Prishi Kumar Nov 14 '17 at 10:16
  • @PrishiKumar i have edited my answer for your last comment – Yegor Babarykin Nov 15 '17 at 16:10
-1

Depending on the keys will be same and display the key for different values you can try some thing like this

public class test {
    public static void main(String args[]){
        HashMap<String, String> map1 = new HashMap<>();
        HashMap<String,String> map2 = new HashMap<>();
        map1.put("Name", "Jerry");
        map1.put("State", "Texas");
        map2.put("Name","TOM");
        map2.put("State", "Texas");
        Iterator<?> it = map1.entrySet().iterator();
        Iterator<?> it1 = map2.entrySet().iterator();
        while (it.hasNext() && it1.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            Map.Entry pair1 = (Map.Entry)it1.next();
            if(!pair.getValue().equals(pair1.getValue())) {
                System.out.println(pair.getKey()+" "+pair.getValue()+" "+pair1.getValue()+" mismatched");
            }
        }
    }
}

Output

Name Jerry TOM mismatched
Lokesh Pandey
  • 1,739
  • 23
  • 50