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
输出为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));输出为true
因为开发需要,只测试了这两种情况。
一.比较两个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
因为开发需要,只测试了这两种情况。
相关文章推荐
- Java录入两个数据,比较这两个数是否相等
- java比较两个文件夹下的文件是否一致
- java比较两个相同对象的属性是否一致
- 比较两个数据库的存储过程定义是否一致V1.0
- Java 比较两个 List 的值是否相等
- 比较任意两个JSON串是否相等(比较对象是否相等)JAVA版
- Java中比较两个对象是否相同
- 第2章 Java编程基础——FAQ2.21 ==和equals()都可用于比较两个操作数是否相等, 它们有什么区别吗?
- java 比较两个对象是否相等
- 比较两个List的数据是否一一对应
- java使用计算md5校验码方式比较两个文件是否相同
- jquery比较两个对象是否一致
- Delphi中比较两个对象是否一致及地址是否相同
- 比较两个目录下文件是否一致(文件夹个数、文件个数、文件大小)
- 比较两个表数据完全一致的sql
- 使用Python的MD5模块比较两个文件是否一致
- java 比较两个文件是否相同
- Java中,比较两个字符串引用变量是否相等
- 二叉树(10)----比较两个二叉树是否相同(结构和数据),递归和非递归
- 比较两个文件是否一致。