我有两个map,一个是我的map;
一个是别人修改的map,
我想找出新map比原来的map有哪些改变。有什么好方法?
------解决方案--------------------
Map 中的方法:
boolean containsKey(Object key)
boolean containsValue(Object value)
------解决方案--------------------
遍历取出原来的map 中的key,value然后用下面的方法比较
------解决方案--------------------
- Java code
Map<String, String> mapA = new HashMap<String, String>(); Map<String, String> mapB = new HashMap<String, String>(); Map<String, String> mapAdd = new HashMap<String, String>(mapB); Map<String, String> mapDel = new HashMap<String, String>(); Map<String, String> mapChange = new HashMap<String, String>(); Map<String, String> mapUnChange = new HashMap<String, String>(); Iterator<String> it = mapA.keySet().iterator(); while(it.hasNext()) { String key = it.next(); String val = mapA.get(key); if(mapB.containsKey(key)) { String bVal = mapB.get(key); if(val != null && val.equals(bVal) || val == bVal) { mapUnChange.put(key, val); } else { mapChange.put(key, val); } mapAdd.remove(key); } else { mapDel.put(key, val); } } }
------解决方案--------------------