Java Guava Vs Apache Commons哈希/Equals构建器
我想知道Guava和ApacheCommons在equals和hashCode构建器方面的关键区别是什么 等于: Apache Commons:Java Guava Vs Apache Commons哈希/Equals构建器,java,guava,apache-commons,Java,Guava,Apache Commons,我想知道Guava和ApacheCommons在equals和hashCode构建器方面的关键区别是什么 等于: Apache Commons: public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (obj.getClass() != getClass()) { return false; } MyCl
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) { return false; }
MyClass other = (MyClass) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(field1, other.field1)
.append(field2, other.field2)
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(field1)
.append(field2)
.toHashCode();
}
番石榴:
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) { return false; }
MyClass other = (MyClass) obj;
return Objects.equal(this.field1, other.field1)
&& Objects.equal(this.field1, other.field1);
}
public int hashCode() {
return Objects.hashCode(field1, field2);
}
哈希代码:
Apache Commons:
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) { return false; }
MyClass other = (MyClass) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(field1, other.field1)
.append(field2, other.field2)
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(field1)
.append(field2)
.toHashCode();
}
番石榴:
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) { return false; }
MyClass other = (MyClass) obj;
return Objects.equal(this.field1, other.field1)
&& Objects.equal(this.field1, other.field1);
}
public int hashCode() {
return Objects.hashCode(field1, field2);
}
其中一个关键区别似乎是Guava版本提高了代码的可读性
我无法从中找到更多信息。如果存在差异,了解更多差异(尤其是性能改进?)将非常有用。我将这种差异称为“存在”。apachecommons中有EqualsBuilder
和HashCodeBuilder
,而Guava中没有构建器。从Guava获得的只是一个实用类MoreObjects
(从Objects
重命名,因为现在JDK中有这样一个类)
Guava方法的优势来自于建筑商的不存在:
- 它不产生垃圾
- 更快
另外请注意,还有一个类似于构建器的组件。在引擎盖下,Guava使用array.hashCode()。Varagrs会对性能进行惩罚,并且有可能自动装箱,这会再次对性能造成影响。根据Java的 如果数组包含其他数组作为元素,则哈希代码基于它们的标识而不是它们的内容 Objects.hasCode的替代品可能是Objects.deepHashCode,但Guava没有使用它。在循环引用的情况下,它有一个缺点 因此,在将自身作为元素包含的数组上调用此方法是不可接受的 通常,ApacheCommons的工作方式更像是deepHashCode,但可能会在图片中添加反射并解决所有上述问题,但(可以说)性能可能会差得多
表单设计透视图ApacheCommons实现了由有效Java项目10和11制定的规则。这给它增加了一种非常不同的感觉Apache创建了一个临时对象,但另一方面它还支持反射式构建快速原型代码。正确。谢谢,本。也考虑一些类似于值对象的东西,让你的<代码>等于<代码/ > >代码> HASCODE < /C> > />代码> ToSoS< <代码> >等。相关:java 7包括,并且像Guava'一样具有可读性,OP的作者仍然犯了错误,比较两次Field1,忘记了Field2(番石榴等)。通过简单的实现,静态代码分析器会发出警报…
对象。hashCode
会分配给varargs,当然也可能分配给自动装箱。还要注意的是,ComparisonChain
看起来像一个构建器,但实际上并没有分配任何东西。我想知道Hotspot需要多长时间来优化它,因为我刚刚重新定义了我的微基准,它仍然是内联实现hashCode的最快方法,使用HashCodeBuilder的速度并没有显著降低,但使用Objects.hash(…)的速度要慢20倍。在最坏的情况下,所有哈希都是int。