java 比较Java中的两个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14053844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:54:00  来源:igfitidea点击:

Comparing two objects in Java

java

提问by schoolcoder

I have two different objects of same entity “Community”

我有同一个实体“社区”的两个不同对象

And two objects(community and com) have same values

并且两个对象(community 和 com)具有相同的值

Communty.java have following variables:

Community.java 有以下变量:

   private Integer communityId;
   private String communityName;
   private String description;

   // many to many relationship
   private Set<Faculty> faculties = new HashSet<Faculty>();
   private Set<User> users = new HashSet<User>();

and I used equal method as:

我使用了相等的方法:

@Override
   public boolean equals(Object obj) {
          // TODO Auto-generated method stub
          if(obj==null)
                 return false;
          if(obj==this)
                 return true;
          if(!(obj instanceof Community)) return false;

          Community community = (Community)obj;
          return community.getCommunityId() == this.getCommunityId();
   }

When I checked community==com, it returns false .. why? What mistake I have done? Both the objects are retrieved from the database!

当我检查时community==com,它返回 false .. 为什么?我犯了什么错误?这两个对象都是从数据库中检索的!

回答by Vi.

==compares links to the objects.

==比较对象的链接。

You should explicitly call community.equals(com). Also take care of the nullchecks.

您应该明确调用community.equals(com). 还要注意检查。

回答by JB Nizet

Because you're comparing objects (the IDs) using ==rather than equals(). ==tests if both variables reference the same object. equals()tests if the two variables reference two functionally equal integers (i.e. with the same int value).

因为您正在使用==而不是比较对象(ID)equals()==测试两个变量是否引用同一个对象。equals()测试两个变量是否引用了两个功能相等的整数(即具有相同的 int 值)。

It's almost always a bug to compare objects using ==, except for enums.

使用 比较对象几乎总是一个错误==,枚举除外。

回答by Rahul

==compares the two references which may be pointing to two different locations irrespective of the object content.

==比较可能指向两个不同位置的两个引用,而不管对象内容如何。

You should use community.equals(com)to check for equality'

您应该使用community.equals(com)来检查相等性'

Also, your equals method contains the following segment :

此外,您的 equals 方法包含以下部分:

community.getCommunityId() == this.getCommunityId()

as communityIdis an Integerobject, the ==operator may give negative result for integer values which are not in the range [-127, 128] due to interning, thats a separate concept, you can check it later.

作为communityId一个Integer对象,==由于实习,运算符可能会对不在 [-127, 128] 范围内的整数值给出否定结果,这是一个单独的概念,您可以稍后检查。

You need to use equals()there as well or compare the .intValue()

您也需要在equals()那里使用或比较 .intValue()

return community.getCommunityId().equals(this.getCommunityId())

回答by aphex

The problem of your equals method is that you are using ==operator for Objects. And here the CommunityId must be the same object in order to return true:

您的 equals 方法的问题在于您正在==为对象使用运算符。这里的 CommunityId 必须是同一个对象才能返回 true:

 return community.getCommunityId() == this.getCommunityId();

It should be

它应该是

 return community.getCommunityId().equals(this.getCommunityId());

回答by Ravi

Because, they don't refer same object. ==used to check, whether both referring same object.

因为,他们不引用同一个对象。==用于检查两者是否引用同一个对象。

==for object refer to same.

==对于对象引用相同。

equalscontent equivalency.

equals内容等价。

try this

试试这个

return community.getCommunityId().equals(this.getCommunityId());

回答by Peter Lawrey

When I checked community==com , it returns false .. why

当我检查 community==com 时,它返回 false .. 为什么

This means; are these two referencesexactly the same. i.e. to the same object. What you intended was

这意味着; 这两个引用完全相同吗?即同一个对象。你想要的是

boolean equal = community.equals(com);

BTW Your if (obj == null)check is redundant.

BTW 你的if (obj == null)支票是多余的。

回答by Nargis

== operator in Java compares the memory address of the two objects,in your case comm and community must be two different objects stored at two different memory addresses

Java 中的 == 运算符比较两个对象的内存地址,在您的情况下,comm 和 community 必须是存储在两个不同内存地址的两个不同对象

回答by Risin

You are comparing two distinct objects communityId's. is it necessary to declare communityId as Integer? because Integer is an object. Why don't you simply declare communityId with a primitive type int; int communityId should work.

您正在比较两个不同的对象 communityId。是否有必要将 communityId 声明为整数?因为 Integer 是一个对象。为什么不简单地用原始类型 int 声明 communityId;int communityId 应该可以工作。