对象比较相等性:JAVA

发布于 2025-01-04 12:54:08 字数 1579 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光下的泡沫是彩色的 2025-01-11 12:54:08

使用 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).

酸甜透明夹心 2025-01-11 12:54:08

您可以在每个方法中实现 compareTo(ClassB) 甚至 compareTo(CommonInterfaceOrSuperclass) 方法,以执行字段级比较并返回 true如果他们符合您的比较标准。然后,您需要迭代列表并检查每个元素。

另一种选择是实现 equals()/hashcode() 对,当编码正确时,它允许您的 List 查找和比较成功实现平等。然后您的 contains() 测试就可以工作了。

You can implement a compareTo(ClassB) or even a compareTo(CommonInterfaceOrSuperclass) method in each that will do the field-level comparison and return true 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 your List to find and compare for equals-ness successfully. Your contains() test would then work.

眼眸里的快感 2025-01-11 12:54:08

尽管内容相同,但它们是不同的对象,不满足相等的条件。您可能需要通过迭代来显式执行 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.

弃爱 2025-01-11 12:54:08

您在这里尝试执行的操作不正确。您要比较的两个对象不仅是不同的对象,而且是不同的类型(ClassAClassB),它们根本没有关系。

此外,如果您尝试像这样进行比较

if(bList.contains(clsA))

,因为您的 bList 包含 ClassB 类型的对象,而 clsA 的类型为 ClassA ,将使用 java.lang.Object 类的 equals() 方法对它们进行比较。
因此,当且仅当 xy 引用同一个对象时,x.equals(y) 返回 truex == 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 and ClassB) which have no relational at all.

Moreover if you try to compare like this

if(bList.contains(clsA))

As your bList contains objects of type ClassB and clsA is of type ClassA, they will be compared using java.lang.Object class's equals() method.
So x.equals(y) returns true if and only if x and y refer to the same object (x == y has the value true).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文