Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对对象使用ComparisonChain有什么好处。equal()&;对象。相等()。。。加番石榴_Java_Equals_Guava_Comparisonchain - Fatal编程技术网

Java 对对象使用ComparisonChain有什么好处。equal()&;对象。相等()。。。加番石榴

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

我刚刚开始使用谷歌的番石榴系列(和)。在我的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),对于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();
    }
    

ComparisonChain旨在帮助对象实现可比或比较器接口

如果您只是实现Object.equals(),那么您是正确的;Objects.equal是您所需要的全部。但如果您正试图实现Comparable或Comparator——正确地——那么使用CompariOnChain要容易得多

考虑:

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);
}