使用 ComparisonChain 相对于 Objects.equal() && 有什么好处? Objects.equal() ...与番石榴
我刚刚开始使用谷歌的番石榴集合(ComparisonChain和对象)。在我的 pojo 中,我重写了 equals 方法,所以我首先这样做了:
return ComparisonChain.start()
.compare(this.id, other.id)
.result() == 0;
但是,我随后意识到我也可以使用这个:
return Objects.equal(this.id, other.id);
:
return Objects.equal(this.name, other.name)
&& Objects.equal(this.number, other.number);
而且我看不到比较链何时会更好,因为您可以轻松添加进一步的条件,如下所示 唯一的好处是我可以看看你是否特别需要返回一个 int 。它有两个额外的方法调用(start 和 result),对于菜鸟来说更复杂。
我缺少的ComparisonChain 是否有明显的好处?
(是的,我还使用适当的 Objects.hashcode()
覆盖 hashcode)
I have just started using google's Guava collection (ComparisonChain and Objects). In my pojo I am overiding the equals method, so I did this first:
return ComparisonChain.start()
.compare(this.id, other.id)
.result() == 0;
However, I then realized that I could also use this :
return Objects.equal(this.id, other.id);
And I fail to see when comparison chain would be better as you can easily add further conditions like so:
return Objects.equal(this.name, other.name)
&& Objects.equal(this.number, other.number);
The only benefit I can see if you specifically need an int returned. It has two extra method calls (start and result) and is more complex to a noob.
Are there obvious benefits of ComparisonChain I missing ?
(Yes, I am also overriding hashcode with appropriate Objects.hashcode()
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ComparisonChain
允许您通过比较多个属性(例如按多列对网格进行排序)来检查一个对象是否小于或大于另一个对象。它应该在实现
Comparable
或Comparator
时使用。Objects.equal
只能检查是否相等。ComparisonChain
allow you to check whether an object is less-than or greater-than another object by comparing multiple properties (like sorting a grid by multiple columns).It should be used when implementing
Comparable
orComparator
.Objects.equal
can only check for equality.ComparisonChain 旨在帮助对象实现 Comparable 或 Comparator 接口。
如果您只是实现 Object.equals(),那么您是对的; Objects.equal 就是您所需要的。但是,如果您尝试正确地实现 Comparable 或 Comparator,那么使用 ComparisonChain 会比其他方式容易得多。
考虑一下:
与实现compareTo相反
...更不用说正确执行该操作的技巧了,这比您想象的要高。 (我见过比你想象的更多搞乱compareTo的方法。)
ComparisonChain is meant to be used in helping objects implement the Comparable or Comparator interfaces.
If you're just implementing Object.equals(), then you're correct; Objects.equal is all you need. But if you're trying to implement Comparable or Comparator -- correctly -- that is much easier with ComparisonChain than otherwise.
Consider:
as opposed to implementing compareTo as
...let alone the trickiness of doing that correctly, which is higher than you'd guess. (I have seen more ways to mess up compareTo than you can imagine.)
使用 Guava 的 ComparisonChain 时要小心,因为它会为每个比较的元素创建一个实例,因此您将看到创建的 N x Log N 比较链只是为了如果您正在排序,则比较;如果您正在迭代并检查相等性,则比较
N
个实例。Java 8 的示例:
如果可能的话,我会使用最新的 Java 8 API 或 Guava 的
Ordering
API 创建一个静态Comparator
,它允许您执行此操作,这里是 如何使用 Guava 的Ordering
API:https://github.com/google/guava/wiki/OrderingExplainedI would be careful when using Guava's
ComparisonChain
because it creates an instance of it per element been compared so you would be looking at a creation ofN x Log N
comparison chains just to compare if you are sorting, orN
instances if you are iterating and checking for equality.I would instead create a static
Comparator
using the newest Java 8 API if possible or Guava'sOrdering
API which allows you to do that, here is an example with Java 8:Here is how to use the Guava's
Ordering
API: https://github.com/google/guava/wiki/OrderingExplained在 POJO 中重写方法的上下文中,我想到了一些 Guava 的工具与一些标准方法相匹配。
Object.equals
使用Objects.equals
进行处理,大致与您提到的Object.hashCode
使用Objects.hashCode like
return Objects.hashCode(id, name);
Comparable.compareTo
使用ComparisonChain
处理,如下所示:In the context of overriding methods in your POJOs, I think of a few of Guava's tools matching with a few standard methods.
Object.equals
is handled usingObjects.equals
in roughly the manner you mentionedObject.hashCode
is handled withObjects.hashCode
likereturn Objects.hashCode(id, name);
Comparable.compareTo
is handled withComparisonChain
as below: