java 使用比较链而不是 Objects.equal() && Objects.equal() 有什么好处......与番石榴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6942096/
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
What is the benefit of using ComparisonChain over Objects.equal() && Objects.equal() ... with Guava
提问by NimChimpsky
I have just started using google's Guava collection (ComparisonChainand Objects). In my pojo I am overiding the equals method, so I did this first:
我刚刚开始使用谷歌的番石榴集合(比较链和对象)。在我的 pojo 中,我覆盖了 equals 方法,所以我先做了这个:
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.
如果您特别需要返回 int,我可以看到的唯一好处。它有两个额外的方法调用(start 和 result),对于菜鸟来说更复杂。
Are there obvious benefits of ComparisonChainI missing ?
我错过了比较链的明显好处吗?
(Yes, I am also overriding hashcode with appropriate Objects.hashcode()
)
(是的,我也在用适当的方式覆盖哈希码Objects.hashcode()
)
回答by SLaks
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
or Comparator
.
ComparisonChain
允许您通过比较多个属性(例如按多列对网格进行排序)来检查对象是否小于或大于另一个对象。
它应该在实现Comparable
或时使用Comparator
。
Objects.equal
can only check for equality.
Objects.equal
只能检查相等性。
回答by Louis Wasserman
ComparisonChain is meant to be used in helping objects implement the Comparable or Comparator interfaces.
比较链旨在帮助对象实现 Comparable 或 Comparator 接口。
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.
如果你只是在实现 Object.equals(),那么你是对的;Objects.equal 就是你所需要的。但是,如果您正在尝试实现 Comparable 或 Comparator —— 正确 —— 使用比较链比其他方式容易得多。
Consider:
考虑:
class Foo implements Comparable<Foo> {
final String field1;
final int field2;
final String field3;
public boolean equals(@Nullable Object o) {
if (o instanceof Foo) {
Foo other = (Foo) o;
return Objects.equal(field1, other.field1)
&& field2 == other.field2
&& Objects.equal(field3, other.field3);
}
return false;
}
public int compareTo(Foo other) {
return ComparisonChain.start()
.compare(field1, other.field1)
.compare(field2, other.field2)
.compare(field3, other.field3)
.result();
}
}
as opposed to implementing compareTo as
与实现 compareTo 相对
int result = field1.compareTo(other.field2);
if (result == 0) {
result = Ints.compare(field2, other.field2);
}
if (result == 0) {
result = field3.compareTo(other.field3);
}
return result;
...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.)
...更不用说正确执行此操作的技巧了,这比您想象的要高。(我见过比你想象的更多的方法来搞砸 compareTo。)
回答by Ray
In the context of overriding methods in your POJOs, I think of a few of Guava's tools matching with a few standard methods.
在你的 POJO 中覆盖方法的上下文中,我想到了一些与一些标准方法匹配的 Guava 工具。
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:public int compareTo(Chimpsky chimpsky) { return ComparisonChain.start() .compare(this.getId(), chimpsky.getId()) .compare(this.getName(), chimpsky.getName()) .result(); }
Object.equals
Objects.equals
大致按照您提到的方式处理Object.hashCode
处理Objects.hashCode
类似return Objects.hashCode(id, name);
Comparable.compareTo
处理ComparisonChain
如下:public int compareTo(Chimpsky chimpsky) { return ComparisonChain.start() .compare(this.getId(), chimpsky.getId()) .compare(this.getName(), chimpsky.getName()) .result(); }
回答by Guido Medina
I 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 of N x Log N
comparison chains just to compare if you are sorting, or N
instances if you are iterating and checking for equality.
使用 Guava 时我会小心,ComparisonChain
因为它会为每个被比较的元素创建一个实例,因此您将查看N x Log N
比较链的创建,只是为了比较您是否正在排序,或者N
如果您正在迭代和检查相等性,则会查看实例。
I would instead create a static Comparator
using the newest Java 8 API if possible or Guava's Ordering
API which allows you to do that, here is an example with Java 8:
Comparator
如果可能的话,我会改为使用最新的 Java 8 API 或Ordering
允许您这样做的Guava 的API创建一个静态,这是 Java 8 的示例:
import java.util.Comparator;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsLast;
private static final Comparator<DomainObject> COMPARATOR=Comparator
.comparingInt(DomainObject::getId)
.thenComparing(DomainObject::getName,nullsLast(naturalOrder()));
@Override
public int compareTo(@NotNull DomainObject other) {
return COMPARATOR.compare(this,other);
}
Here is how to use the Guava's Ordering
API: https://github.com/google/guava/wiki/OrderingExplained
以下是如何使用 Guava 的Ordering
API:https: //github.com/google/guava/wiki/OrderingExplained