Java 对对象使用ComparisonChain有什么好处。equal()&;对象。相等()。。。加番石榴
我刚刚开始使用谷歌的番石榴系列(和)。在我的pojo中,我过度使用了equals方法,因此我首先做了以下工作:Java 对对象使用ComparisonChain有什么好处。equal()&;对象。相等()。。。加番石榴,java,equals,guava,comparisonchain,Java,Equals,Guava,Comparisonchain,我刚刚开始使用谷歌的番石榴系列(和)。在我的pojo中,我过度使用了equals方法,因此我首先做了以下工作: return ComparisonChain.start() .compare(this.id, other.id) .result() == 0; 然而,我后来意识到我也可以用这个: return Objects.equal(this.id, other.id); 我看不出什么时候比较链会更好,因为您可以很容易地添加其他条件,如: return
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),对于noob来说更复杂
比较有什么明显的好处吗
(是的,我还使用适当的
对象覆盖hashcode。hashcode()
)ComparisonChain
允许您通过比较多个属性(如按多列排序网格)来检查一个对象是否小于或大于另一个对象。在实现
可比
或比较器
时应使用它
对象。equal
只能检查是否相等。在您的POJO中覆盖方法的上下文中,我想到了一些与一些标准方法相匹配的Guava工具
使用Object.equals
处理,大致与您提到的方式相同Objects.equals
与Object.hashCode
类似对象一起处理。hashCode
返回对象。hashCode(id,name)代码>
通过compariable。comparieto
处理,如下所示:ComparisonChain
public int compareTo(Chimpsky chimpsky) { return ComparisonChain.start() .compare(this.getId(), chimpsky.getId()) .compare(this.getName(), chimpsky.getName()) .result(); }
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();
}
}
…更不用说正确操作的技巧了,这比你想象的要高。(我已经看到了比你想象的更多的弄乱compareTo的方法。)在使用番石榴的
ComparisonChain
时,我会小心,因为它会为每个被比较的元素创建一个实例,所以你会看到创建N x Log N
比较链,以便在排序时进行比较,或N
实例,如果您正在迭代并检查是否相等
如果可能的话,我将使用最新的Java 8 API或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);
}
import java.util.Comparator;
导入静态java.util.Comparator.naturalOrder;
导入静态java.util.Comparator.nullsLast;
专用静态最终比较器比较器=比较器
.comparingit(域对象::getId)
.then比较(DomainObject::getName,nullsLast(naturalOrder());
@凌驾
public int compareTo(@NotNull DomainObject other){
返回比较器。比较(这个,其他);
}
下面是如何使用番石榴的
排序API:从来没有理由编写?true:false
在Java中。您不能让Compariable
接受null
,但是如果您使用比较器,那么将其作为排序来编写可以让您执行排序.nullsFirst()
或排序.nullsLast()
来指定它应该如何排序空值。
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);
}