比较Java中两个对象列表的最佳方法
我有两个对象列表比较Java中两个对象列表的最佳方法,java,list,dictionary,hashmap,Java,List,Dictionary,Hashmap,我有两个对象列表 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 Cu
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字段组成的唯一键在两个对象中相同时,我才能比较对象内部的值
目前,我正在创建两个表示两个列表的哈希映射
Map<CustomKey, CustomObj>
Map
然后,我迭代第一个hashmap,对于每个键,我检查该键是否存在于其他hashmap中,如果存在,我从其他hashmap中获取对象,然后比较这两个对象
有更好的方法吗?基于前五个键重写equals和hashcode方法。 然后您可以使用equals方法来比较对象和
使用集合批量操作,如retainAll等。基于前五个键重写equals和hashcode方法。 然后您可以使用equals方法来比较对象和
使用集合批量操作,如retainAll等。您可以覆盖CustomObj的Equals和HashCode。
然后使用Contains()测试唯一性。您可以覆盖CustomObj的Equals和HashCode。
然后使用Contains()测试唯一性。希望这能帮助您,这是您的课程:
equal
方法,在实现中,我将类CustomObj
的属性与CustomKey
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;
}
}
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
}
CustomKey:
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;
}
}
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
}
希望这对你有所帮助,这是你的课程:
equal
方法,在实现中,我将类CustomObj
的属性与CustomKey
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;
}
}
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
}
CustomKey:
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;
}
}
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
}
我建议使用
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
类似
实现这些方法后,您可以在哈希集合中安全地使用这些对象。我建议使用
对象.equals()
和对象.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
类似
在实现这些方法之后,您可以在哈希集合中安全地使用这些对象。您可以向代码展示如何比较它们吗?然后我们可以告诉你它是否正确。你必须重写equals/hashcode方法让我们假设我有两个映射,即从对象列表生成的objMap1和objMap2对于(Map.Entry-Entry:objMap1.entrySet()){CustomKey-key=Entry.getKey();CustomObject a=Entry.getValue();CustomObject b=objMap2.get(key);}`由于我现在有两个具有相同键的对象,所以我可以比较这些对象的值。这是什么意思“只有当两个对象中由key1-key5字段组成的唯一键相同时,我才需要比较对象内部的值"? 听起来你无论如何都需要比较所有的字段来确定两个对象是相等的。你能告诉代码你是如何比较它们的吗?然后我们可以告诉你它是否正确。你必须重写equals/hashcode方法让我们假设我有两个映射,即从对象列表生成的objMap1和objMap2对于(Map.Entry-Entry:objMap1.entrySet()){CustomKey-key=Entry.getKey();CustomObject a=Entry.getValue();CustomObject b=objMap2.get(key);}`由于我现在有两个具有相同键的对象,所以我可以比较这些对象的值。这是什么意思“只有当两个对象中由key1-key5字段组成的唯一键相同时,我才需要比较对象内部的值"? 听起来你们无论如何都需要比较所有的字段来确定两个对象是相等的。这是一个很好的尝试,但你们的方法过于复杂了。您的代码可能会导致
ClassCastException
,因为您没有执行instanceof
检查。这是一个很好的尝试,但您的方法过于复杂。您的代码可能会导致ClassCastException
,因为您没有执行instanceof
检查。