Java中如何比较两个HashMap中的数据是否相等呢?
下文笔者讲述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
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


