java Guava 与 Apache Commons Hash/Equals 构建器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31058353/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:01:16  来源:igfitidea点击:

Guava Vs Apache Commons Hash/Equals builders

javaguavaapache-commons

提问by Abhishek

I was wondering what are key differences between Guava vs Apache Commons with respect to equals and hashCode builders.

我想知道 Guava 与 Apache Commons 在 equals 和 hashCode 构建器方面的主要区别是什么。

equals:

等于

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

Guava:

番石榴:

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

hashCode:

哈希码

Apache Commons:

阿帕奇公地:

public int hashCode() {
    return new HashCodeBuilder(17, 37)
            .append(field1)
            .append(field2)
            .toHashCode();
}

Guava:

番石榴:

public int hashCode() {
    return Objects.hashCode(field1, field2);
}

One of the key difference appears to be improved code readability with Guava's version.

主要区别之一似乎是提高了 Guava 版本的代码可读性。

I couldn't find more information from https://code.google.com/p/guava-libraries/wiki/CommonObjectUtilitiesExplained. It would be useful to know more differences (especially any performance improvement?) if there are any.

我无法从https://code.google.com/p/guava-libraries/wiki/CommonObjectUtilitiesExplained找到更多信息。如果有的话,了解更多差异(尤其是任何性能改进?)会很有用。

回答by maaartinus

I'd call this difference "existence". There are EqualsBuilderand HashCodeBuilderin Apache Commons and there are no builders in Guava. All you get from Guava is a utility class MoreObjects(renamed from Objectsas there's such a class in JDK now).

我将这种差异称为“存在”。有EqualsBuilderHashCodeBuilder在Apache的百科全书,并有番石榴没有建设者。你从 Guava 得到的只是一个实用程序类MoreObjectsObjects因为现在 JDK 中有这样一个类,所以改名了)。

The advantages of Guava's approach come from the non-existence of the builder:

Guava 方法的优势来自于 builder 的不存在:

  • it produces no garbage
  • it's faster
  • 它不产生垃圾
  • 它更快

The JIT compiler can possibly eliminate the garbage via Escape Analysisand also the associated overhead. Then they get equally fast as they do exactly the same.

JIT 编译器可以通过转义分析消除垃圾以及相关的开销。然后他们变得同样快,因为他们做的完全一样。

I personally find the builders slightly more readable. If you find not using them better, then Guava is surely the right thing for you. As you can see, the static methods are good enough for the task.

我个人认为构建器更具可读性。如果您发现没有更好地使用它们,那么 Guava 对您来说肯定是正确的选择。如您所见,静态方法足以完成任务。

Note also that there's also a ComparisonChainwhich is a sort of Comparable-builder.

另请注意,还有一个比较链,它是一种 Comparable-builder。

回答by Arunas Junevicius

Under the hood Guava uses Arrays.hashCode(). Varagrs impose performance penalty and there is potential for autoboxing, which again would impose performance hit. According to Java's documentation

在幕后 Guava 使用 Arrays.hashCode()。Varagrs 会造成性能损失,并且存在自动装箱的可能性,这会再次造成性能损失。根据Java的文档

If the array contains other arrays as elements, the hash code is based on their identities rather than their contents

如果数组包含其他数组作为元素,则哈希码基于它们的身份而不是它们的内容

Alternative to Objects.hasCode might be Objects.deepHashCode but it's not employed by Guava. And it has a drawback in case of circular references

Objects.hasCode 的替代品可能是 Objects.deepHashCode,但它没有被 Guava 使用。在循环引用的情况下它有一个缺点

It is therefore unacceptable to invoke this method on an array that contains itself as an element

因此,在包含自身作为元素的数组上调用此方法是不可接受的

Normally Apache Commons works more like deepHashCode, but might add reflection to the picture and tackle all the aforementioned issues, but (arguably) can suffer from much worse performance.

通常 Apache Commons 的工作方式更像 deepHashCode,但可能会为图片添加反射并解决上述所有问题,但(可以说)可能会遭受更糟糕的性能。

Form design perspective Apache Commons implements rules laid out by Effective Java items 10 and 11. Which adds a very different feel to it

表单设计视角 Apache Commons 实现了 Effective Java 第 10 条和第 11 条规定的规则。这给它增添了非常不同的感觉