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

  • <tfoot id='brUpL'></tfoot>
    <legend id='brUpL'><style id='brUpL'><dir id='brUpL'><q id='brUpL'></q></dir></style></legend>
    • <bdo id='brUpL'></bdo><ul id='brUpL'></ul>

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

        如何定义我自己的元素类以与 Set 一起使用

        How do define my own element class for use with Set(如何定义我自己的元素类以与 Set 一起使用)
          <tbody id='ldYG0'></tbody>

              <legend id='ldYG0'><style id='ldYG0'><dir id='ldYG0'><q id='ldYG0'></q></dir></style></legend>
            1. <tfoot id='ldYG0'></tfoot>

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

                  <bdo id='ldYG0'></bdo><ul id='ldYG0'></ul>
                  <i id='ldYG0'><tr id='ldYG0'><dt id='ldYG0'><q id='ldYG0'><span id='ldYG0'><b id='ldYG0'><form id='ldYG0'><ins id='ldYG0'></ins><ul id='ldYG0'></ul><sub id='ldYG0'></sub></form><legend id='ldYG0'></legend><bdo id='ldYG0'><pre id='ldYG0'><center id='ldYG0'></center></pre></bdo></b><th id='ldYG0'></th></span></q></dt></tr></i><div id='ldYG0'><tfoot id='ldYG0'></tfoot><dl id='ldYG0'><fieldset id='ldYG0'></fieldset></dl></div>
                  本文介绍了如何定义我自己的元素类以与 Set 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下代码:

                  public class MyElement {
                      String name;
                      String type;
                  
                      MyElement(String name, String type) {
                          this.name = name;
                          this.type = type;
                      }
                  }
                  
                  public class Test {
                  
                      public static void main(String[] args) {
                          Set<MyElement> set = new HashSet<MyElement>();
                          set.add(new MyElement("foo", "bar"));
                          set.add(new MyElement("foo", "bar"));
                          set.add(new MyElement("foo", "bar"));
                          System.out.println(set.size());
                          System.out.println(set.contains(new MyElement("foo", "bar")));
                      }
                  }
                  

                  执行时返回:

                  3
                  
                  false
                  

                  我本来希望结果为 1 且为真.为什么我的元素不被认为是相同的,我该如何纠正这个问题?谢谢,韦恩.

                  I would have expected the result to be 1 and true. Why are my elements not being recognised as being the same and how do I rectify this? Thanks, Wayne.

                  推荐答案

                  您需要根据通用合约在 MyElement 上实现 equals(Object o)hashCode().如果没有 Set.contains() 将使用比较对象的内存地址的默认实现.由于您在 contains 调用中创建了 MyElement 的新实例,因此它返回为 false.

                  You need to implement equals(Object o) and hashCode() on MyElement per the general contract. Absent that Set.contains() will use the default implementation which compares the memory address of the objects. Since you're creating a new instance of MyElement in the contains call it comes back as false.

                  这篇关于如何定义我自己的元素类以与 Set 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

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

                      <tbody id='qy7Qj'></tbody>

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

                        • <tfoot id='qy7Qj'></tfoot>

                          • <i id='qy7Qj'><tr id='qy7Qj'><dt id='qy7Qj'><q id='qy7Qj'><span id='qy7Qj'><b id='qy7Qj'><form id='qy7Qj'><ins id='qy7Qj'></ins><ul id='qy7Qj'></ul><sub id='qy7Qj'></sub></form><legend id='qy7Qj'></legend><bdo id='qy7Qj'><pre id='qy7Qj'><center id='qy7Qj'></center></pre></bdo></b><th id='qy7Qj'></th></span></q></dt></tr></i><div id='qy7Qj'><tfoot id='qy7Qj'></tfoot><dl id='qy7Qj'><fieldset id='qy7Qj'></fieldset></dl></div>
                          • <legend id='qy7Qj'><style id='qy7Qj'><dir id='qy7Qj'><q id='qy7Qj'></q></dir></style></legend>