比较两个map中的key value,有什么简便的方法或思路?

damao11 2014-02-07 11:31:06
RT:比较两个map中的key value,有什么简便的方法或思路?
假如拿map2与map1做比较;实现
map1和map2中存在完全相同的key和value则返回true

map1包含map2中的key和value也返回true

map1不包含map2中的key返回false 并打印该key和value

...全文
4171 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
teemai 2014-02-08
  • 打赏
  • 举报
回复
直接循环比较吧。 或者重写equals方法,吧key和value都比较下,都相同返回true,否则返回false
Mich_LY 2014-02-08
  • 打赏
  • 举报
回复
引用 4 楼 fangmingshijie 的回复:
Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface. Overrides: equals(...) in Object Parameters: o object to be compared for equality with this map map的equals方法说的很明白了。
equals只能比较两个map是否key值相同,不能判断是否是包含关系,楼主需要包含时也返回true
  • 打赏
  • 举报
回复
Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface. Overrides: equals(...) in Object Parameters: o object to be compared for equality with this map map的equals方法说的很明白了。
  • 打赏
  • 举报
回复
俩map直接equals就行了。
tony4geek 2014-02-08
  • 打赏
  • 举报
回复
循环比较好了。
Mich_LY 2014-02-08
  • 打赏
  • 举报
回复
	public static boolean compareMap(Map map1, Map map2) {
		boolean contain = false;
		for (Object o : map1.keySet()) {
			contain = map2.containsKey(o);
			if (contain) {
				contain = map1.get(o).equals(map2.get(o));
			}
			if (!contain) {
				return false;
			}
		}
		return true;
	}
仅限基础数据类型的map集合,对象类型需要重写equals和hashcode
damao11 2014-02-08
  • 打赏
  • 举报
回复
引用 5 楼 u012974494 的回复:
[quote=引用 4 楼 fangmingshijie 的回复:] Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface. Overrides: equals(...) in Object Parameters: o object to be compared for equality with this map map的equals方法说的很明白了。
equals只能比较两个map是否key值相同,不能判断是否是包含关系,楼主需要包含时也返回true[/quote] 包含关系和精确比对 分别这样实现也可以:
		//比对map1(实际文本)和map2(预期文本)
		boolean contain = false;
			//包含关系先遍历map2,再判断map1中是否包含map2
		    for (Object o : map2.keySet()) {       
		    	contain = map1.containsKey(o);        
		    	if (contain) {            
		    		contain = map2.get(o).equals(map1.get(o));  
		    		}        
		    	if (!contain) {           
		    		System.out.println("校验内容失败,实际不包含预期");       
		    		}   
		    	} 
		//比对map1(实际文本)和map2(预期文本)
		boolean contain = false;
			//精确比对先遍历map1,再判断map2中是否包含map1
		    for (Object o : map1.keySet()) { 
		    	contain = map2.containsKey(o);        
		    	if (contain) {            
		    		contain = map1.get(o).equals(map2.get(o));  
		    		}        
		    	if (!contain) {           
		    		System.out.println("校验内容失败,预期不等于实际");      
		    		}   
		    	} 
		}

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧