两个很大的HashMap,要求列出key不在另一个map中的以及value不同的所有entry,求一个比较高效的算法。例:
map1
[a, 1]
[b, 2]
[c, 3]
[d, 4]map2
[b, 3]
[c, 2]
[d, 4]
[e, 5]result:
**in map1 not in map2***
[a,1]**in map2 not in map1***
[e,5]**value mismatch**
map1:[b, 2]  map2:[b, 3]
map1:[c, 3]  map2:[c, 2]

解决方案 »

  1.   

    貌似用jdk自带的方法就可实现
      

  2.   

    Map map = new HashMap(map1)
    Set set = map.keySet();
    set.removeAll(map2.keySet());
    System.out.println("**in map1 not in map2***");
    System.out.println(map);
    map1.keySet().removeAll(map.keySet());map = new HashMap(map2);
    set = map.keySet();
    set.removeAll(map1.keySet());
    System.out.println("**in map2 not in map1***");
    System.out.println(map);
    map2.keySet().removeAll(map.keySet());Map map = new HashMap(map1);
    map.entrySet().retainAll(map2.entrySet());
    map1.entrySet().removeAll(map.entrySet());
    map2.entrySet().removeAll(map.entrySet());
    System.out.println("**value mismatch**");
    System.out.println(map1);
    System.out.println(map2);