简体   繁体   English

java.util.Objects.equals和com.google.common.base.Objects.equals有什么区别?

[英]What is the difference between java.util.Objects.equals and com.google.common.base.Objects.equals?

是否有任何理由使用Guava而不是java.util实现?

java.util.Objects and its equals method only exist since Java 7. The Guava version predates it. java.util.Objects及其equals方法仅在Java 7之后存在.Guava版本早于它。 If you are using at least Java 7, there's no reason to use the Guava version. 如果您至少使用Java 7,则没有理由使用Guava版本。

One difference besides the fact that the Guava method predates the Java 7 one: the Guava method is called equal while the Java method is called equals . 除了Guava方法早于Java 7之外,还有一个区别:Guava方法被称为equal而Java方法被称为equals This is actually significant because it means the Java version can't be used with a static import. 这实际上很重要,因为这意味着Java版本不能与静态导入一起使用。 (Even ignoring that, I think equal is a better name for a static method that takes two arguments and compares them for equality, while equals is better for an instance method like Object.equals .) (即使忽略这一点,我觉得equal是有两个参数并对它们进行比较平等的静态方法更好的名字,而equals是像一个实例方法更好Object.equals 。)

All that said, if you're on JDK7 you should probably use the java.util.Objects version. 总而言之,如果您使用的是JDK7,则应该使用java.util.Objects版本。

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

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