平等对象比较:JAVA [英] Object comparison for equality : JAVA

查看:95
本文介绍了平等对象比较: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个类绝对相同(除了名称当然),我添加到两个数组列表:aListA和aListB。我需要比较两个对象是否相同。如果他们是相同的,我需要将它们添加到另一个列表(commonList),如果他们碰巧是不同的,我需要将它们添加到另一个列表(不同的列表)。我写了以下代码:

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);
}
}

我的问题是,即使数据两个变量,firstId和secondID)在两个不同的POJO是相同的,他们被放在不同的列表。这意味着对象不是使用containsList()方法的ArrayList类进行比较我不想得到是否有一种方法可以比较两个变量的POJO,并确定为等于或不等于?

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?

推荐答案

事件内容是相同的,它们是不同的对象,不satifsy等条件。你可能需要做显式的a.getfirstId()。equals(b.getfirsId())和a.getscondID (b.getSecondId())。

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.

另一种可能性是在两个POJO类中覆盖equals和hashcode方法。请参阅此链接,了解 equals / hashcode合同

The other possibility may be overriding equals and hashcode methods in both POJO classes. Refer this link for equals/hashcode contract.

这篇关于平等对象比较:JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆