• <legend id='uaw2U'><style id='uaw2U'><dir id='uaw2U'><q id='uaw2U'></q></dir></style></legend>

    <small id='uaw2U'></small><noframes id='uaw2U'>

    1. <i id='uaw2U'><tr id='uaw2U'><dt id='uaw2U'><q id='uaw2U'><span id='uaw2U'><b id='uaw2U'><form id='uaw2U'><ins id='uaw2U'></ins><ul id='uaw2U'></ul><sub id='uaw2U'></sub></form><legend id='uaw2U'></legend><bdo id='uaw2U'><pre id='uaw2U'><center id='uaw2U'></center></pre></bdo></b><th id='uaw2U'></th></span></q></dt></tr></i><div id='uaw2U'><tfoot id='uaw2U'></tfoot><dl id='uaw2U'><fieldset id='uaw2U'></fieldset></dl></div>

          <bdo id='uaw2U'></bdo><ul id='uaw2U'></ul>
      1. <tfoot id='uaw2U'></tfoot>
      2. HashSet contains() 方法

        时间:2023-10-14

        <small id='zx1D1'></small><noframes id='zx1D1'>

      3. <tfoot id='zx1D1'></tfoot>
          <i id='zx1D1'><tr id='zx1D1'><dt id='zx1D1'><q id='zx1D1'><span id='zx1D1'><b id='zx1D1'><form id='zx1D1'><ins id='zx1D1'></ins><ul id='zx1D1'></ul><sub id='zx1D1'></sub></form><legend id='zx1D1'></legend><bdo id='zx1D1'><pre id='zx1D1'><center id='zx1D1'></center></pre></bdo></b><th id='zx1D1'></th></span></q></dt></tr></i><div id='zx1D1'><tfoot id='zx1D1'></tfoot><dl id='zx1D1'><fieldset id='zx1D1'></fieldset></dl></div>

          • <legend id='zx1D1'><style id='zx1D1'><dir id='zx1D1'><q id='zx1D1'></q></dir></style></legend>

                <bdo id='zx1D1'></bdo><ul id='zx1D1'></ul>

                    <tbody id='zx1D1'></tbody>
                1. 本文介绍了HashSet contains() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我执行了下面的代码,发现输出是 false.

                  I executed below code and found the output was false.

                  import java.util.Set;
                  import java.util.HashSet;
                  
                  public class Name {
                      private String first, last;
                  
                      public Name(String first, String last) {
                          this.first = first;
                          this.last = last;
                      }
                  
                      public boolean equals(Object o) {
                          if (!(o instanceof Name))
                              return false;
                          Name n = (Name) o;
                          return n.first.equals(first) && n.last.equals(last);
                      }
                  
                      public static void main(String[] args) {
                          Set<Name> s = new HashSet<Name>();
                          s.add(new Name("Donald", "Duck"));
                          System.out.println(s.contains(new Name("Donald", "Duck")));
                      }
                  }
                  

                  我想知道它的行为方式以及为什么输出为 false.

                  I want to know how it behaves and why output is false.

                  推荐答案

                  您还需要重写 hashCode() 方法以及 equals().这两种方法都用于 HashSet 的正确功能,因此如果您将 class 作为key,否则 Object 类的 hashCode() 正在被使用,没有两个不同的 objects 可以被视为相同因为它们的 hashCode() 总是不同的,并且在 contains() 的情况下肯定会返回 false.

                  You need to override hashCode() method also along with equals(). Both the methods are used for proper functionality of HashSet, so must be overriden in a user-defined class if you are making that class as a key, else hashCode() of Object class is getting used and no two different objects can be considered as same as their hashCode() would always be different, and will for sure return falsealways in the case of contains().

                  这篇关于HashSet contains() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Common Lisp 是否有类似 java 的 Set Interface/implementing classes 下一篇:LinkedHashSet 删除重复对象

                  相关文章

                    <bdo id='DRPcr'></bdo><ul id='DRPcr'></ul>

                2. <legend id='DRPcr'><style id='DRPcr'><dir id='DRPcr'><q id='DRPcr'></q></dir></style></legend>

                  <small id='DRPcr'></small><noframes id='DRPcr'>

                    <tfoot id='DRPcr'></tfoot>

                    1. <i id='DRPcr'><tr id='DRPcr'><dt id='DRPcr'><q id='DRPcr'><span id='DRPcr'><b id='DRPcr'><form id='DRPcr'><ins id='DRPcr'></ins><ul id='DRPcr'></ul><sub id='DRPcr'></sub></form><legend id='DRPcr'></legend><bdo id='DRPcr'><pre id='DRPcr'><center id='DRPcr'></center></pre></bdo></b><th id='DRPcr'></th></span></q></dt></tr></i><div id='DRPcr'><tfoot id='DRPcr'></tfoot><dl id='DRPcr'><fieldset id='DRPcr'></fieldset></dl></div>