# 1.3-常見Object方法
[原文鏈接](http://code.google.com/p/guava-libraries/wiki/CommonObjectUtilitiesExplained)?**譯者:** 沈義揚
**equals**
當一個對象中的字段可以為null時,實現Object.equals方法會很痛苦,因為不得不分別對它們進行null檢查。使用[`Objects.equal`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Objects.html#equal%28java.lang.Object, java.lang.Object%29)幫助你執行null敏感的equals判斷,從而避免拋出NullPointerException。例如:
```
Objects.equal("a", "a"); // returns true
Objects.equal(null, "a"); // returns false
Objects.equal("a", null); // returns false
Objects.equal(null, null); // returns true
```
_注意:JDK7引入的Objects類提供了一樣的方法_[`_Objects.equals_`](http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#equals%28java.lang.Object, java.lang.Object%29)_。_
**hashCode**
用對象的所有字段作散列[hash]運算應當更簡單。Guava的[`Objects.hashCode(Object...)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Objects.html#hashCode%28java.lang.Object...%29)會對傳入的字段序列計算出合理的、順序敏感的散列值。你可以使用Objects.hashCode(field1, field2, …, fieldn)來代替手動計算散列值。
_注意:JDK7引入的Objects類提供了一樣的方法_[`_Objects.hash(Object...)_`](http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#hash%28java.lang.Object...%29)
**toString**
好的toString方法在調試時是無價之寶,但是編寫toString方法有時候卻很痛苦。使用 [Objects.toStringHelper](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Objects.html#toStringHelper%28java.lang.Object%29)可以輕松編寫有用的toString方法。例如:
```
// Returns "ClassName{x=1}"
Objects.toStringHelper(this).add("x", 1).toString();
// Returns "MyObject{x=1}"
Objects.toStringHelper("MyObject").add("x", 1).toString();
```
**compare/compareTo**
實現一個比較器[Comparator],或者直接實現Comparable接口有時也傷不起。考慮一下這種情況:
```
class Person implements Comparable<Person> {
private String lastName;
private String firstName;
private int zipCode;
public int compareTo(Person other) {
int cmp = lastName.compareTo(other.lastName);
if (cmp != 0) {
return cmp;
}
cmp = firstName.compareTo(other.firstName);
if (cmp != 0) {
return cmp;
}
return Integer.compare(zipCode, other.zipCode);
}
}
```
這部分代碼太瑣碎了,因此很容易搞亂,也很難調試。我們應該能把這種代碼變得更優雅,為此,Guava提供了[`ComparisonChain`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ComparisonChain.html)。
ComparisonChain執行一種懶比較:它執行比較操作直至發現非零的結果,在那之后的比較輸入將被忽略。
```
public int compareTo(Foo that) {
return ComparisonChain.start()
.compare(this.aString, that.aString)
.compare(this.anInt, that.anInt)
.compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
.result();
}
```
這種[Fluent接口](http://en.wikipedia.org/wiki/Fluent_interface)風格的可讀性更高,發生錯誤編碼的幾率更小,并且能避免做不必要的工作。更多Guava排序器工具可以在下一節里找到。
- Google Guava官方教程(中文版)
- 1-基本工具
- 1.1-使用和避免null
- 1.2-前置條件
- 1.3-常見Object方法
- 1.4-排序: Guava強大的”流暢風格比較器”
- 1.5-Throwables:簡化異常和錯誤的傳播與檢查
- 2-集合
- 2.1-不可變集合
- 2.2-新集合類型
- 2.3-強大的集合工具類:java.util.Collections中未包含的集合工具
- 2.4-集合擴展工具類
- 3-緩存
- 4-函數式編程
- 5-并發
- 5.1-google Guava包的ListenableFuture解析
- 5.2-Google-Guava Concurrent包里的Service框架淺析
- 6-字符串處理:分割,連接,填充
- 7-原生類型
- 9-I/O
- 10-散列
- 11-事件總線
- 12-數學運算
- 13-反射