我有两个对象列表。
public class CustomObj {
Long key1;
Integer key2;
Integer key3;
Integer key4;
Integer key5;
BigDecimal value1
BigDecimal value2
BigDecimal value3
BigDecimal value4
BigDecimal value5; }
public class CustomKey {
Long key1;
Integer key2;
Integer key3;
Integer key4;
Integer key5; }
对象没有唯一标识符,但是来自对象的5个字段key1到key5为对象创建了唯一的键。我必须比较这两个对象列表。只有在两个对象中由key1-key5字段组成的唯一键相同时,我才必须比较对象内的值。
目前我正在创建两个代表两个列表的HashMaps。
Map<CustomKey, CustomObj>
然后,我迭代第一个hashmap,为每个键检查该键是否存在于其他hashmap中,如果存在,我从其他hashmap获取对象,然后比较这两个对象。
有没有更好的方法呢?
答案 0 :(得分:4)
根据前五个键重写equals和hashcode方法。 然后你可以使用equals方法来比较对象和 使用集合批量操作,如retainAll等。
答案 1 :(得分:3)
您可以覆盖CustomObj的Equals和HashCode。 然后使用Contains()来测试唯一性。
答案 2 :(得分:2)
我建议使用Objects.equals()
和Objects.hash()
来实现相等性检查和哈希码计算。它们是零安全的,易于编写和理解:
public class CustomKey {
Long key1;
Integer key2;
Integer key3;
Integer key4;
Integer key5;
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof CustomKey)) return false;
CustomKey k = (CustomKey) o;
return Objects.equals(key1, k.key1)
&& Objects.equals(key2, k.key2)
&& Objects.equals(key3, k.key3)
&& Objects.equals(key4, k.key4)
&& Objects.equals(key5, k.key5);
}
@Override
public int hashCode() {
return Objects.hash(key1, key2, key3, key4, key5);
}
}
与CustomObj
类似。
实现这些方法后,您可以安全地在哈希集合中使用这些对象。
答案 3 :(得分:0)
希望这会帮助你,这是你上课:
equal
方法,在实现中,我正在使用CustomObj
CustomKey
的属性进行比较
醇>
<强> CustomObj:强>
public class CustomObj {
Long key1;
Integer key2;
Integer key3;
Integer key4;
Integer key5;
BigDecimal value1;
BigDecimal value2;
BigDecimal value3;
BigDecimal value4;
BigDecimal value5;
public CustomObj(Long k1, Integer k2, Integer k3, Integer k4, Integer k5){
this.key1 = k1;
this.key2 = k2;
this.key3 = k3;
this.key4 = k4;
this.key5 = k5;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key1 == null) ? 0 : key1.hashCode());
result = prime * result + ((key2 == null) ? 0 : key2.hashCode());
result = prime * result + ((key3 == null) ? 0 : key3.hashCode());
result = prime * result + ((key4 == null) ? 0 : key4.hashCode());
result = prime * result + ((key5 == null) ? 0 : key5.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
CustomKey other = (CustomKey) obj;
if (key1 == null) {
if (other.key1 != null)
return false;
} else if (!key1.equals(other.key1))
return false;
if (key2 == null) {
if (other.key2 != null)
return false;
} else if (!key2.equals(other.key2))
return false;
if (key3 == null) {
if (other.key3 != null)
return false;
} else if (!key3.equals(other.key3))
return false;
if (key4 == null) {
if (other.key4 != null)
return false;
} else if (!key4.equals(other.key4))
return false;
if (key5 == null) {
if (other.key5 != null)
return false;
} else if (!key5.equals(other.key5))
return false;
return true;
}
}
<强> CustomKey:强>
public class CustomKey {
Long key1;
Integer key2;
Integer key3;
Integer key4;
Integer key5;
public CustomKey(Long k1, Integer k2, Integer k3, Integer k4, Integer k5){
this.key1 = k1;
this.key2 = k2;
this.key3 = k3;
this.key4 = k4;
this.key5 = k5;
}
}
To Test it:
public static void main(String[] args) {
CustomObj customObj = new CustomObj(123L, 11, 34, 45, 99);
CustomKey customKey = new CustomKey(123L, 11, 34, 45, 99);
CustomKey customKey2 = new CustomKey(124L, 12, 34, 45, 99);
System.out.println(customObj.equals(customKey));// This will return true since the key is same
System.out.println(customObj.equals(customKey2));// This will return false since the key is same
}