简体   繁体   English

getter 可以用在 equals 和 hashcode 中吗?

[英]can getters be used in equals and hashcode?

I have below code which overrides equals() and hashcode() methods.我有下面的代码覆盖 equals() 和 hashcode() 方法。

public boolean equals(Object obj)
 {
   if (obj == null)
     return false;
   if (!(obj instanceof Name))
     return false;
   Name name = (Name) obj;
   return this.name.equals(name.name);
 }

 public int hashCode()
 {
   return name.hashCode();
 }

here can i replace below 2 lines:我可以在这里替换以下 2 行:

return this.name.equals(name.name);
return name.hashCode();

with

return this.getName().equals(name.getName());
return getName().hashCode();

i mean instead of using properties can i directly use getters inside equals and hashcode methods?我的意思是我可以直接在 equals 和 hashcode 方法中使用 getter 而不是使用属性吗?

Thanks!谢谢!

Yes sure you could use this,的,你可以使用这个,

hashcode() and equals() are field methods, They can access private members directly, but what if there is some logic wrapped in accessor method, so it is always safe to access fields using accessor methods hashcode()equals()是字段方法,它们可以直接访问私有成员,但是如果访问方法中包含一些逻辑怎么办,那么使用访问方法访问字段总是安全的

Yes, I strongly recommend using getters in equals() & hashCode() implementation.是的,我强烈建议在equals() & hashCode()实现中使用 getter。 And to the best of my knowledge, this will not harm you in any manner.据我所知,这不会以任何方式伤害您。

The implementation of equals() without getters will fail to give you the correct result when you compare two proxied objects.当您比较两个代理对象时,没有 getter 的equals()实现将无法为您提供正确的结果。

Eg: If you try comparing two proxied objects returned by your underlying hibernate dao, you'll never receive true from your equals() method, even though they're same objects.例如:如果您尝试比较底层 hibernate dao 返回的两个代理对象,您将永远不会从equals()方法收到true ,即使它们是相同的对象。

Update更新

Use getter methods instead of direct access.使用 getter 方法而不是直接访问。 This is because the object instance passed as other may be a proxy object, not the actual instance.这是因为作为 other 传递的 object 实例可能是代理 object,而不是实际实例。 To initialize this proxy, you need to access it with a getter method.要初始化此代理,您需要使用 getter 方法访问它。

Check this for more information.检查以获取更多信息。

You can, but why would you?你可以,但你为什么要这样做? Option A: the compiler inlines that, so you end up with a reference to the field anyway.选项 A:编译器将其内联,因此无论如何您最终都会得到对该字段的引用。 Option B: The compiler does not inline the call, ie you've introduced one extra method call.选项 B:编译器不内联调用,即您引入了一个额外的方法调用。

There are also implications for legibility- if the name field is directly accessible within the class, why not refer to it directly?易读性也有影响——如果在 class 中可以直接访问name字段,为什么不直接引用它呢? I find this easier to read, but some people find it inconsistent.我发现这更容易阅读,但有些人发现它不一致。

Yes, why would you not be able to?是的,你为什么不能?

In future, with things like this, I recommend you just give it a try and see.以后遇到这样的事情,还是建议大家试试看。

Yes you can.是的你可以。 Why not?为什么不? What's your doubts?你有什么疑问?

No.不。

Fields used in hashCode() should be final value types. hashCode()中使用的字段应该是final值类型。 If they aren't, the hash of the instance can change after it has been added to a map or set which will lead to runtime errors like Map not being able to find an instance that you just put into it.如果不是,实例的 hash 在添加到 map 或 set 后可能会发生变化,这将导致运行时错误,例如Map无法找到您刚刚放入其中的实例。

Since equals() must behave the same as hashCode() , this requirement leaks into equals() as well.由于equals()的行为必须与hashCode()相同,因此此要求也会泄漏到equals()中。 Since the field is final , subclasses should not overwrite the getter, so they should be final as well.由于字段是final的,子类不应该覆盖 getter,因此它们也应该是final的。

When the getter is final , there is no point to use it in equals() / hashCode() : It's more characters to type, more code to read, makes compilation slower (the compiler must consider to inline this), might make execution slower.当 getter 是final时,没有必要在equals() / hashCode()中使用它:输入更多的字符,读取更多的代码,使编译变慢(编译器必须考虑内联它),可能会使执行变慢.

For more information about the many pitfalls of equals()/hashCode(), see the excellent EqualsVerifier .有关 equals()/hashCode() 的许多陷阱的更多信息,请参阅优秀的EqualsVerifier

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM