您的位置:首页 > 编程语言 > Java开发

Java 比较两个HashMap里的数据是否一致

2017-06-30 10:36 459 查看
开发中,可能会遇到两个HashMap内容的比较,如果一一去遍历比较的话,可能造成资源的浪费。现可以采用一下方法:

一.比较两个HashMap<String,TestBean> 内容是否一致

1.首先创建一个TestBean实体,记得重写hashCode  和  equals 方法。

package com.wxd.test;

public class TestBean {
private String id = "";
private String dc = "";
private String status = "";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDc() {
return dc;
}
public void setDc(String dc) {
this.dc = dc;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "TestBean [id=" + id + ", dc=" + dc + ", status=" + status + "]";
}
public TestBean(String id, String dc, String status) {
super();
this.id = id;
this.dc = dc;
this.status = status;
}
public TestBean() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dc == null) ? 0 : dc.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((status == null) ? 0 : status.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;
TestBean other = (TestBean) obj;
if (dc == null) {
if (other.dc != null)
return false;
} else if (!dc.equals(other.dc))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (status == null) {
if (other.status != null)
return false;
} else if (!status.equals(other.status))
return false;
return true;
}

}

2.进行比较
TestBean t1 = new TestBean("1", "1", "1");
TestBean t2 = new TestBean("1", "1", "1");
HashMap<String,TestBean> map1 = new HashMap();
HashMap<String,TestBean> map2 = new HashMap();
map1.put("1", t1);
map2.put("1", t2);
System.out.println(map1.equals(map2));输出为 true
TestBean t1 = new TestBean("1", "2", "1");
TestBean t2 = new TestBean("1", "1", "1");
HashMap<String,TestBean> map1 = new HashMap();
HashMap<String,TestBean> map2 = new HashMap();
map1.put("1", t1);
map2.put("1", t2);
System.out.println(map1.equals(map2));输出为false

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

HashMap<String,TestBean> map1 = new HashMap();
HashMap<String,TestBean> 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");
// 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));输出为true
因为开发需要,只测试了这两种情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: