Javaは二つのHashMapのデータが一致しているかどうかを比較します。
4091 ワード
開発中に、二つのHashMapの内容の比較があるかもしれません。一つ一つを比べてみたら、資源の無駄遣いになるかもしれません。今は方法を採用してもいいです。
一.二つのHashMapの内容が一致しているかどうかを比較する。
1.まずはTestBeanエンティティを作成し、hashCodeを書き換えてください。 和 equals方法
二つのHashMapの中の入れ子mapを比較する。
開発に必要なので、この二つの状況だけをテストしました。
一.二つのHashMapの内容が一致しているかどうかを比較する。
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 map1 = new HashMap();
HashMap 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 map1 = new HashMap();
HashMap 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 map1 = new HashMap();
HashMap 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 tmap1 = new HashMap();
HashMap tmap2 = new HashMap();
tmap1.put("1", "1");
tmap2.put("1", "1");
tmap1.put("2", "1");
HashMap> map1 = new HashMap();
HashMap> map2 = new HashMap();
map1.put("1", tmap1);
map2.put("1", tmap2);
System.out.println(map1.equals(map2));
出力はfalseです。 HashMap tmap1 = new HashMap();
HashMap tmap2 = new HashMap();
tmap1.put("1", "1");
tmap2.put("1", "1");
// tmap1.put("2", "1");
HashMap> map1 = new HashMap();
HashMap> map2 = new HashMap();
map1.put("1", tmap1);
map2.put("1", tmap2);
System.out.println(map1.equals(map2));
出力はtrueです。開発に必要なので、この二つの状況だけをテストしました。