📄 lib0068.html
字号:
</div>
<p class="para">
<i class="emphasis">Source</i>: src/book/sample/general/EqualsDemonstration.java</p>
<a name="315"></a><a name="IDX-127"></a>
<p class="para">
<a class="internaljump" href="#ch10list4a">Listing 10.4a</a> uses a simple class that does not override method <span class="fixed">equals()</span> and uses the implementation inherited from <span class="fixed">Object</span>. The variable declared in line 3 with the value 5 should be "equal" to the object declared in line 5. However, if you were to run the sample, you would see that the variables are actually not considered equal. Output to the sample is provided in <a class="internaljump" href="#ch10list4b">listing 10.4b</a>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 10.4b: </span>Output from Listing 10.4a</span><a name="316"></a><a name="ch10list4b"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
Object equals() demo:
fiveAsObject.equals(anotherFiveAsObject): false
fiveAsObject.equals(sevenAsObject): false
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">If you were to run a different sample using class <span class="fixed">String</span> instead of <span class="fixed">ObjectWithoutEqualsImpl</span>, the output would be more what you would expect because <span class="fixed">String</span> overrides method <span class="fixed">equals()</span>.</p>
<p class="para">The method <span class="fixed">hashcode()</span> returns an integer that is guaranteed to be equal for two instances of <span class="fixed">Object</span> that are equal. The logic behind constructing an algorithm to do this can get intricate. I usually utilize <span class="fixed">String</span>, which has a nice implementation of <span class="fixed">hashcode()</span>. You can concatenate all fields of a value object and get the hashcode of the resulting string. It is legal to have <span class="fixed">hashcode()</span> return the same integer for all instances, but this will make using <span class="fixed">HashMap</span> and <span class="fixed">Hashtable</span> extremely inefficient. <a class="internaljump" href="#ch10list05">Listing 10.5</a> illustrates an effective implementation of <span class="fixed">hashcode()</span>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 10.5: </span>Sample hashcode() Implementation</span><a name="317"></a><a name="ch10list05"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1: public int hashcode()
2: {
3: return this.getObjectAsString().hashCode();
4: }
5:
6: private String getObjectAsString()
7: {
8: return this.getObjectAsString(this);
9: }
10:
11: private String getObjectAsString(CustomerVO vo)
12: {
13: StringBuffer buffer = new StringBuffer(256);
14:
15: if (vo._customerId != null)
16: {
17: buffer.append(vo._customerId);<a name="318"></a><a name="IDX-128"></a>
18: }
19: else buffer.append("null");
20: if (vo._firstName != null)
21: {
22: buffer.append(vo._firstName);
23: }
24: else buffer.append("null");
25: if (vo._lastName != null)
26: {
27: buffer.append(vo._lastName);
28: }
29: else buffer.append("null");
30: if (vo._address != null)
31: {
32: buffer.append(vo._address);
33: }
34: else buffer.append("null");
35: if (vo._city != null) buffer.append(vo._city);
36: else buffer.append("null");
37: if (vo._state != null) buffer.append(vo._state);
38: else buffer.append("null");
39: if (vo._zipCode != null)
40: {
41: buffer.append(vo._zipCode);
42: }
43: else buffer.append("null");
44:
45: return buffer.toString();
46: }
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/vo/ CustomerVO.java</p>
<p class="para">Implementing <span class="fixed">equals()</span> is similar. You concatenate all field members and use the <span class="fixed">equals()</span> implementation of <span class="fixed">String</span>, as shown in <a class="internaljump" href="#ch10list06">listing 10.6</a>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 10.6: </span>Sample equals() Implementation</span><a name="319"></a><a name="ch10list06"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1: public boolean equals(Object obj)
2: {
3: boolean answer = false;
4:
5: if (obj instanceof CustomerVO)
6: {
7: String dtoId =
8: this.getObjectAsString( (CustomerVO) obj);
9: if (this.getObjectAsString().equals(dtoId))
10: {
11: answer = true;
12: }<a name="320"></a><a name="IDX-129"></a>
13: }
14:
15: return answer;
16: }
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/vo/ CustomerVO.java</p>
<p class="para">
<b class="bold">Consider implementing </b><b class="bold"><span class="fixed">java.lang.Comparable</span></b>. If you ever use a value object in a sorted collection (e.g., <span class="fixed">TreeSet</span> or <span class="fixed">TreeMap</span>), you must implement <span class="fixed">Comparable</span> for sensible sort results. Implementing <span class="fixed">Comparable</span> requires the implementation of a <span class="fixed">compareTo()</span> method that returns 0 if the two objects are equal, a negative number if the object is less than the argument passed, or a positive number if the <span class="fixed">Object</span> is greater than the argument passed. <a class="internaljump" href="#ch10list07">Listing 10.7</a> illustrates.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 10.7: </span>Sample compareTo() Implementation</span><a name="321"></a><a name="ch10list07"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1: public int compareTo(Object obj)
2: {
3: int compareResult = 0;
4: Object tempObj = null;
5: Object tempObjCompareTarget = null;
6: Comparable c1, c2;
7:
8: if (obj == null)
9: {
10: throw new IllegalArgumentException
11: ("Comparisons to null objects not defined.");
12: }
13: if (! (obj instanceof CustomerVO))
14: {
15: throw new IllegalArgumentException
16: ("Comparing different class types not allowed.");
17: }
18:
19: CustomerVO dto = (CustomerVO) obj;
20: compareResult = _lastName.compareTo(dto._lastName);
21: if (compareResult == 0)
22: {
23: compareResult =
24: _firstName.compareTo(dto._firstName);
25: }
26: if (compareResult == 0)
27: {
28: compareResult =
29: _customerId.compareTo(dto._customerId);
30: }
31: if (compareResult == 0)<a name="322"></a><a name="IDX-130"></a>
32: {
33: compareResult =
34: _address.compareTo(dto._address);
35: }
36: if (compareResult == 0)
37: {
38: compareResult =
39: _city.compareTo(dto._city);
40: }
41: if (compareResult == 0)
42: {
43: compareResult = _state.compareTo(dto._state);
44: }
45: if (compareResult == 0)
46: {
47: compareResult =
48: _zipCode.compareTo(dto._zipCode);
49: }
50:
51: return compareResult;
52: }
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="last-para">
<i class="emphasis">Source:</i> /src/book/sample/vo/ CustomerVO.java</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0067.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0069.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -