Java中如何比较两个HashMap中的数据是否相等呢?

乔欣 Java经验 发布时间:2023-03-30 06:20:17 阅读数:9165 1
下文笔者讲述java中比较两个HashMap中数据是否相等的方法分享,如下所示

HashMap中数据是否相等的实现思路

 
HashMap相等的实现思路:
     方式1:
	   遍历HashMap中的元素
	 方式2:
	   重写HashMap中实体类的hashCode和equals方法
	    然后直接使用hashmap对象的equals方法进行对比
例:HashMap对象是否相等对比

//创建实体类TestClass实体
//重写hashCode 和 equals方法 

package com.java265.test;
 
public class TestClass {
	private String a = "";
	private String b = "";
	private String c = "";
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
     ...
    
	@Override
	public String toString() {
		return "TestClass [a=" + a + ", b=" + b + ", c=" + c + "]";
	}
	public TestClass(String a, String b, String c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}
	public TestClass() {
		super();
 	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((a == null) ? 0 : a.hashCode());
		result = prime * result + ((b == null) ? 0 : b.hashCode());
		result = prime * result + ((c == null) ? 0 : c.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TestClass other = (TestClass) obj;
		if (b == null) {
			if (other.b != null)
				return false;
		} else if (!b.equals(other.b))
			return false;
		
		
		if (a == null) {
			if (other.a != null)
				return false;
		} else if (!id.equals(other.a))
			return false;
		
		if (c == null) {
			if (other.c != null)
				return false;
		} else if (!c.equals(other.c))
			return false;
		
		return true;
	}
	
}

//HashMap对比示例
   
TestClass t1 = new TestClass("1", "1", "1");
TestClass t2 = new TestClass("1", "1", "1");
HashMap<String,TestClass> map1 = new HashMap();
HashMap<String,TestClass> map2 = new HashMap();
map1.put("1", t1);
map2.put("1", t2);
System.out.println(map1.equals(map2));

//输出为 true

TestClass t1 = new TestClass("1", "2", "1");
TestClass t2 = new TestClass("1", "1", "1");
HashMap<String,TestClass> map1 = new HashMap();
HashMap<String,TestClass> map2 = new HashMap();
map1.put("1", t1);
map2.put("1", t2);
System.out.println(map1.equals(map2));
//输出为false

TestClass t1 = new TestClass("1", "2", "1");
TestClass t2 = new TestClass("1", "1", "1");
TestClass t3 = new TestClass("1", "1", "1");

HashMap<String,TestClass> map1 = new HashMap();
HashMap<String,TestClass> map2 = new HashMap();
map1.put("1", t1);
map1.put("2", t3);
map2.put("1", t2);
System.out.println(map1.equals(map2));
//输出为false

HashMap中嵌套map的对比示例

HashMap<String,String> tmap1 =  new HashMap();
HashMap<String,String> tmap2 =  new HashMap();
tmap1.put("1", "1");
tmap2.put("1", "1");
tmap1.put("2", "1");
HashMap<String,HashMap<String,String>> map1 = new HashMap();
HashMap<String,HashMap<String,String>> map2 = new HashMap();
map1.put("1", tmap1);
map2.put("1", tmap2);
System.out.println(map1.equals(map2));
//输出为false

HashMap<String,String> tmap1 =  new HashMap();
HashMap<String,String> tmap2 =  new HashMap();
tmap1.put("1", "1");
tmap2.put("1", "1");

HashMap<String,HashMap<String,String>> map1 = new HashMap();
HashMap<String,HashMap<String,String>> map2 = new HashMap();
map1.put("1", tmap1);
map2.put("1", tmap2);
System.out.println(map1.equals(map2));
//输出为true
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202303/16801284616175.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者