对象比较相等性:JAVA
public ClassA
{
private String firstId;
private String secondId;
public void setFirstId(String firstId) {
this.firstId = firstId;
}
public String getFirstId() {
return id;
}
public void setSecondId(String secondId) {
this.secondId = secondId;
}
public String getSecondId() {
return secondId;
}
}
public ClassB
{
private String firstId;
private String secondId;
public void setFirstId(String firstId) {
this.firstId = firstId;
}
public String getFirstId() {
return id;
}
public void setSecondId(String secondId) {
this.secondId = secondId;
}
public String getSecondId() {
return secondId;
}
}
我有上述 2 个类(POJO),它们完全相同(当然名称除外),我将它们添加到两个数组列表中:aListA 和 aListB。我需要比较两个对象是否相同。如果它们相同,我需要将它们添加到另一个列表(commonList),如果它们碰巧不同,我需要将它们添加到另一个列表(differenceList)。我编写了以下代码:
ClassA clsA = null;
public ArrayList genCommonList(aList, bList)
{
for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(bList.contains(clsA)
{
genCommonList.add(clsA);
}
}
public ArrayList genDifferentList(aList, bList)
{
for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(!bList.contains(clsA)
{
genDifferentList.add(clsA);
}
}
我的问题是,即使两个不同 POJO 中的数据(变量,firstId 和 secondaryID)相同,它们也会被放入不同的列表中。这意味着不会使用 ArrayList 类的 contains() 方法比较对象的相等性。我不想从 POJO 内的每个变量获取数据并进行比较。有没有办法比较两个变量的 POJO 并确定相等或不相等?
public ClassA
{
private String firstId;
private String secondId;
public void setFirstId(String firstId) {
this.firstId = firstId;
}
public String getFirstId() {
return id;
}
public void setSecondId(String secondId) {
this.secondId = secondId;
}
public String getSecondId() {
return secondId;
}
}
public ClassB
{
private String firstId;
private String secondId;
public void setFirstId(String firstId) {
this.firstId = firstId;
}
public String getFirstId() {
return id;
}
public void setSecondId(String secondId) {
this.secondId = secondId;
}
public String getSecondId() {
return secondId;
}
}
I have the above 2 classes(POJOs) which are absolutely the same (except for the name of course) which I am adding to two arraylists: aListA and aListB. I need to compare if the two objects are the same. If they are same I need to add them to another list(commonList) and if they happen to be the different, I need to add them to another list(differentList). I have written the following code:
ClassA clsA = null;
public ArrayList genCommonList(aList, bList)
{
for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(bList.contains(clsA)
{
genCommonList.add(clsA);
}
}
public ArrayList genDifferentList(aList, bList)
{
for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(!bList.contains(clsA)
{
genDifferentList.add(clsA);
}
}
My problem is that even when the data(both variables, firstId and secondID) in the 2 different POJOs are the same, they get put in the different list. That means the objects are not being compared for equality using the contains() method of the ArrayList class. I dont want to get the data from each variable inside the POJO and compare. Is there a way to compare the POJOs for both variables and be determined as either equal or unequal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Object.equals 的默认实现,不同类的两个对象永远不会相同。
如果您想要这种行为,则需要覆盖类中的 .equals(以及 .hashcode,请查看 javadoc)。
(或者制作一个比较器并将其传递给您的集合)。
With the default implementation of Object.equals, two objects of different classes will never be the same.
If you want that behavior, you will need to override .equals in your classes (and .hashcode as well, look at the javadoc).
(Or make a comparator and pass it to your collections).
您可以在每个方法中实现
compareTo(ClassB)
甚至compareTo(CommonInterfaceOrSuperclass)
方法,以执行字段级比较并返回true
如果他们符合您的比较标准。然后,您需要迭代列表并检查每个元素。另一种选择是实现
equals()
/hashcode()
对,当编码正确时,它允许您的List
查找和比较成功实现平等。然后您的contains()
测试就可以工作了。You can implement a
compareTo(ClassB)
or even acompareTo(CommonInterfaceOrSuperclass)
method in each that will do the field-level comparison and returntrue
if they meet your comparison criteria. You would then need to iterate over your lists and check against each element.Another alternative is to implement the
equals()
/hashcode()
pair, which would, when coded properly, allow yourList
to find and compare for equals-ness successfully. Yourcontains()
test would then work.尽管内容相同,但它们是不同的对象,不满足相等的条件。您可能需要通过迭代来显式执行 a.getfirstId().equals(b.getfirsId()) 和 a.getscondID().equals(b.getSecondId()) 。
另一种可能性可能是重写两个 POJO 类中的 equals 和 hashcode 方法。请参阅此链接以获取equals/hashcode 合约。
Eventhough content is same they are different objects which doesn't satifsy equal condition. You may need to do explicit a.getfirstId().equals(b.getfirsId()) and a.getscondID().equals(b.getSecondId()) by iterating.
The other possibility may be overriding equals and hashcode methods in both POJO classes. Refer this link for equals/hashcode contract.
您在这里尝试执行的操作不正确。您要比较的两个对象不仅是不同的对象,而且是不同的类型(
ClassA
和ClassB
),它们根本没有关系。此外,如果您尝试像这样进行比较
,因为您的
bList
包含ClassB
类型的对象,而clsA
的类型为ClassA
,将使用 java.lang.Object 类的 equals() 方法对它们进行比较。因此,当且仅当
x
和y
引用同一个对象时,x.equals(y)
返回true
(x == y
的值为true
)。What you are trying to do here in not correct. The 2 objects that you are comparing are not only different objects but are also of different types (
ClassA
andClassB
) which have no relational at all.Moreover if you try to compare like this
As your
bList
contains objects of typeClassB
andclsA
is of typeClassA
, they will be compared usingjava.lang.Object
class'sequals()
method.So
x.equals(y)
returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).