从equals方法开始说

<p>一句废话,eqauls方法是用来判断对象是否相等的。<br>怎么判断的呢?可以重写类的equals方法,在equals方法中实现判等逻辑。<br>但是没有重写equals方法,怎么判断两个对象是否相等呢?根据java的继承机制,咱们去找它爸爸,去看看父类是怎么实现的equals方法。类的老祖宗Ojbect中,equals方法实现十分简单,如下:<br> <figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/</span></div><div class="line"> * <span class="doctag">@param</span> obj the reference object with which to compare.</div><div class="line"> * <span class="doctag">@return</span> &lt;code&gt;true&lt;/code&gt; if this object is the same as the obj</div><div class="line"> * argument; &lt;code&gt;false&lt;/code&gt; otherwise.</div><div class="line"> * <span class="doctag">@see</span> #hashCode()</div><div class="line"> * <span class="doctag">@see</span> java.util.Hashtable</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span> </span>&#123;</div><div class="line"> <span class="keyword">return</span> (<span class="keyword">this</span> == obj);</div><div class="line"> &#125;</div></pre></td></tr></table></figure></p>
<p>通俗易懂,就是判断两个对象地址是否相等。<br>看下面一例:<br>定义student类<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</div><div class="line"> <span class="comment">/
</span></div><div class="line"> * 学号</div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> String stu_no;</div><div class="line"> <span class="comment">/</span></div><div class="line"> * 姓名</div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> String name;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">()</span> </span>&#123;</div><div class="line"> &#125;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(String no, String name)</span> </span>&#123;</div><div class="line"> <span class="keyword">this</span>.stu_no = no;</div><div class="line"> <span class="keyword">this</span>.name = name;</div><div class="line"> &#125;</div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getStu_no</span><span class="params">()</span> </span>&#123;</div><div class="line"> <span class="keyword">return</span> stu_no;</div><div class="line"> &#125;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setStu_no</span><span class="params">(String stu_no)</span> </span>&#123;</div><div class="line"> <span class="keyword">this</span>.stu_no = stu_no;</div><div class="line"> &#125;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</div><div class="line"> <span class="keyword">return</span> name;</div><div class="line"> &#125;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</div><div class="line"> <span class="keyword">this</span>.name = name;</div><div class="line"> &#125;</div><div class="line"></div><div class="line"> <span class="meta">@Override</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object obj)</span> </span>&#123;</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">this</span> == obj) &#123;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">null</span> == obj) &#123;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> &#125; <span class="keyword">else</span> <span class="keyword">if</span> (!(obj <span class="keyword">instanceof</span> Student)) &#123;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>.name.equals(((Student) obj).getName())</div><div class="line"> &amp;&amp; <span class="keyword">this</span>.stu_no.equals(((Student) obj).getStu_no())) &#123;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> &#125;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"></div><div class="line"> &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>执行测试类:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line"> Set&lt;Student&gt; set = <span class="keyword">new</span> HashSet&lt;Student&gt;();</div><div class="line"> Student stu1 = <span class="keyword">new</span> Student(<span class="string">"1"</span>, <span class="string">"张三"</span>);</div><div class="line"> set.add(stu1);</div><div class="line"> Student stu2 = <span class="keyword">new</span> Student(<span class="string">"1"</span>, <span class="string">"张三"</span>);</div><div class="line"> set.add(stu2);</div><div class="line"> System.out.println(<span class="string">"是否equal:"</span> + stu1.equals(stu2));</div><div class="line"> System.out.println(<span class="string">"set大小:"</span> + set.size());</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>结果输出:<br>是否equal:true<br>set大小:2<br>为啥set里放进了两个equal的对象;答:因为hashCode不同;<br>对呀,还有hashCode这回事;<br>先来说明为什么set中放入了两个equal的对象;因为在向set中添加数据的时候先检查两个对象的hashCode是否相等,如果不等的话,会直接将对i想放入到集合中。如果两个对象的hashCode相等时,才会去调用equals方法判断是否相等;<br>那为什么没有重写hashCode方法,他们的hashCode值就不同能。再到父类中去寻找hashCode的实现。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/
</span></div><div class="line"> * As much as is reasonably practical, the hashCode method defined by </div><div class="line"> * class &lt;tt&gt;Object&lt;/tt&gt; does return distinct integers for distinct </div><div class="line"> * objects. (This is typically implemented by converting the internal </div><div class="line"> * address of the object into an integer, but this implementation </div><div class="line"> * technique is not required by the </div><div class="line"> * Java&lt;font size="-2"&gt;&lt;sup&gt;TM&lt;/sup&gt;&lt;/font&gt; programming language.)</div><div class="line"> *</div><div class="line"> * <span class="doctag">@return</span> a hash code value for this object.</div><div class="line"> * <span class="doctag">@see</span> java.lang.Object#equals(java.lang.Object)</div><div class="line"> * <span class="doctag">@see</span> java.util.Hashtable</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">native</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span></span>;</div></pre></td></tr></table></figure></p>
<p>可以看到这是调用一个本地方法;在注释中发现Object的hashCode会对每个对象返回唯一的hash值。<br>so,在没有重写hashCode方法的时候两个Student的hash值肯定是不同的。所以两个对象都可以放到set中。<br>那么什么情况下要重写equals方法和hashCode方法呢?</p>

创作不易,转载请注明文章来源为www.full-satcker.com:邢桂帅 » 从equals方法开始说

赞 (0)
分享到:更多 ()

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. SunshineLife is short, and this article saved vaullbae time on this Earth.回复