Skip to content
Snippets Groups Projects
Commit ae18412a authored by Goik Martin's avatar Goik Martin
Browse files

Hash code

parent 5931a140
No related branches found
No related tags found
No related merge requests found
Doc/Sd1/Ref/CoreClasses/Hash/iceCreamPrices.jpg

101 KiB

......@@ -2,6 +2,7 @@
<chapter annotations="slide" version="5.1" xml:id="sw1ChapterCoreClasses"
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:ns="http://docbook.org/ns/transclusion"
......@@ -75,13 +76,131 @@
String s2 = new String("Kate");
System.out.println("s1 == s2: " + (s1 == s2));
System.out.println("s1.equals(s2): " + s1.equals(s2));
</programlisting>
System.out.println("s1.equals(s2): " + s1.equals(s2));</programlisting>
<screen>s1 == s2: false
s1.equals(s2): true</screen>
</figure>
<figure xml:id="sd1_coreclasses_fig_hashPrinciple">
<title>Hashing principle</title>
<informaltable border="0">
<tr>
<td valign="top"><mediaobject>
<imageobject>
<imagedata fileref="Ref/CoreClasses/Hash/iceCreamPrices.jpg"/>
</imageobject>
</mediaobject></td>
<td valign="middle"><quote>I want the 12p one</quote></td>
</tr>
</informaltable>
</figure>
<figure xml:id="sd1_coreclasses_fig_hashExample">
<title><classname>Rectangle</classname>
<methodname>equals(...)</methodname> and
<methodname>hashCode()</methodname></title>
<programlisting language="java">public class Rectangle {
int width, height;
@Override public boolean equals(Object o) {
if (o instanceof Rectangle) {
return width == ((Rectangle) o).width
&amp;&amp; height == ((Rectangle) o).height;
} else {
return false;
}
}
@Override public int hashCode() {
return width + height;
}
}</programlisting>
</figure>
<figure xml:id="sd1_coreclasses_fig_hashRectangleValues">
<title><classname>Rectangle</classname> hash values</title>
<informaltable border="1">
<tr>
<th>width</th>
<th>height</th>
<th>hash value</th>
</tr>
<tr>
<td valign="top">1</td>
<td valign="top">3</td>
<td valign="top">4</td>
</tr>
<tr>
<td valign="top">2</td>
<td valign="top">2</td>
<td valign="top">4</td>
</tr>
<tr>
<td valign="top">5</td>
<td valign="top">5</td>
<td valign="top">10</td>
</tr>
<tr>
<td valign="top">2</td>
<td valign="top">7</td>
<td valign="top">9</td>
</tr>
<tr>
<td valign="top">4</td>
<td valign="top">9</td>
<td valign="top">13</td>
</tr>
</informaltable>
</figure>
<figure xml:id="sd1_coreclasses_fig_hashRectangleBetter">
<title><classname>Rectangle</classname>
<methodname>equals(...)</methodname> and
<methodname>hashCode()</methodname></title>
<programlisting language="java">public class Rectangle {
int width, height;
...
@Override public int hashCode() {
return width + <emphasis role="red">13 * height</emphasis>;
}
}</programlisting>
</figure>
<figure xml:id="sd1_coreclasses_fig_hashEqualsContract">
<title><classname>Contract
</classname><methodname>equals(...)</methodname> and
<methodname>hashCode()</methodname></title>
<para>If <methodname>a.equals(b)</methodname> ==&gt; <code>a.hashCode() ==
b.hashCode()</code>. <emphasis role="red">The reverse does not
apply!</emphasis></para>
<para>Consequence: <methodname>equals()</methodname> and
<methodname>hashCode()</methodname> must be <emphasis role="red">redefined
simultaneously</emphasis>!</para>
</figure>
<section xml:id="sd1SectVarargsFormat">
<title>Reconsidering System.out.format().</title>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment