将 2 个 HashMap 与作为字符串的键和作为用户定义对象的值进行比较
Comparing 2 HashMap with Key as String and Value as UserDefined Object
我想输出一个布尔值作为 true 表示两个映射具有相同的键和值。
如果我使用 equals() 它 returns false。
我如何输出为 true ,对象引用不同。但是条目是一样的
我下面有2张地图
Map<String,Information> map1=new HashMap<>();
map1.put("key1", new Information("10","20","30","40"));
map1.put("key2", new Information("11","22","33","44"));
Map<String,Information> map2=new HashMap<>();
map2.put("key1", new Information("10","20","30","40"));
map2.put("key2", new Information("11","22","33","44"));
POJO 如下:具有 public getter 和 setter
public class Information {
private String value1;
private String value2;
private String value3;
private String value4;
public Information(String value1,
String value2,
String value3,
String value4)
{
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
}
你必须像这样喂同样的东西
Information info1 = new Information("10", "20", "30", "40");
Information info2 = new Information("11", "22", "33", "44");
Map<String, Information> map1 = new HashMap<>();
map1.put("key1", info1);
map1.put("key2", info2);
Map<String, Information> map2 = new HashMap<>();
map2.put("key1", info1);
map2.put("key2", info2);
System.out.println(map2.equals(map1));// prints true
你得到 false 因为你正在比较 Information 类的两个不同实例。
A HashMap
使用 equals()
比较两个条目。对于 HashMap<String, Information>
,它使用 String
的 equals()
和 Information
来确定两个条目是否相等。由于您的 Information
class 不会从 Object
覆盖 equals()
,因此相等性比较基于地址。
要按值比较两个 Information
,您可以在 Information
中覆盖 equals()
class:
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (obj instanceof Information info) {
return value1.equals(info.value1) &&
value2.equals(info.value2) &&
value3.equals(info.value3) &&
value4.equals(info.value4);
}
return false;
}
我想输出一个布尔值作为 true 表示两个映射具有相同的键和值。 如果我使用 equals() 它 returns false。 我如何输出为 true ,对象引用不同。但是条目是一样的 我下面有2张地图
Map<String,Information> map1=new HashMap<>();
map1.put("key1", new Information("10","20","30","40"));
map1.put("key2", new Information("11","22","33","44"));
Map<String,Information> map2=new HashMap<>();
map2.put("key1", new Information("10","20","30","40"));
map2.put("key2", new Information("11","22","33","44"));
POJO 如下:具有 public getter 和 setter
public class Information {
private String value1;
private String value2;
private String value3;
private String value4;
public Information(String value1,
String value2,
String value3,
String value4)
{
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
}
你必须像这样喂同样的东西
Information info1 = new Information("10", "20", "30", "40");
Information info2 = new Information("11", "22", "33", "44");
Map<String, Information> map1 = new HashMap<>();
map1.put("key1", info1);
map1.put("key2", info2);
Map<String, Information> map2 = new HashMap<>();
map2.put("key1", info1);
map2.put("key2", info2);
System.out.println(map2.equals(map1));// prints true
你得到 false 因为你正在比较 Information 类的两个不同实例。
A HashMap
使用 equals()
比较两个条目。对于 HashMap<String, Information>
,它使用 String
的 equals()
和 Information
来确定两个条目是否相等。由于您的 Information
class 不会从 Object
覆盖 equals()
,因此相等性比较基于地址。
要按值比较两个 Information
,您可以在 Information
中覆盖 equals()
class:
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (obj instanceof Information info) {
return value1.equals(info.value1) &&
value2.equals(info.value2) &&
value3.equals(info.value3) &&
value4.equals(info.value4);
}
return false;
}