• <small id='nhJ2Z'></small><noframes id='nhJ2Z'>

    1. <tfoot id='nhJ2Z'></tfoot>

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

          <bdo id='nhJ2Z'></bdo><ul id='nhJ2Z'></ul>
        <i id='nhJ2Z'><tr id='nhJ2Z'><dt id='nhJ2Z'><q id='nhJ2Z'><span id='nhJ2Z'><b id='nhJ2Z'><form id='nhJ2Z'><ins id='nhJ2Z'></ins><ul id='nhJ2Z'></ul><sub id='nhJ2Z'></sub></form><legend id='nhJ2Z'></legend><bdo id='nhJ2Z'><pre id='nhJ2Z'><center id='nhJ2Z'></center></pre></bdo></b><th id='nhJ2Z'></th></span></q></dt></tr></i><div id='nhJ2Z'><tfoot id='nhJ2Z'></tfoot><dl id='nhJ2Z'><fieldset id='nhJ2Z'></fieldset></dl></div>
      1. 从 Set 中获取元素

        Getting an element from a Set(从 Set 中获取元素)
      2. <tfoot id='sIvCl'></tfoot>
          <tbody id='sIvCl'></tbody>
      3. <i id='sIvCl'><tr id='sIvCl'><dt id='sIvCl'><q id='sIvCl'><span id='sIvCl'><b id='sIvCl'><form id='sIvCl'><ins id='sIvCl'></ins><ul id='sIvCl'></ul><sub id='sIvCl'></sub></form><legend id='sIvCl'></legend><bdo id='sIvCl'><pre id='sIvCl'><center id='sIvCl'></center></pre></bdo></b><th id='sIvCl'></th></span></q></dt></tr></i><div id='sIvCl'><tfoot id='sIvCl'></tfoot><dl id='sIvCl'><fieldset id='sIvCl'></fieldset></dl></div>

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

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

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

                  本文介绍了从 Set 中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Set 为什么不提供一个操作来获取一个等于另一个元素的元素?

                  Why doesn't Set provide an operation to get an element that equals another element?

                  Set<Foo> set = ...;
                  ...
                  Foo foo = new Foo(1, 2, 3);
                  Foo bar = set.get(foo);   // get the Foo element from the Set that equals foo
                  

                  我可以问 Set 是否包含一个等于 bar 的元素,那为什么我不能得到那个元素呢?:(

                  I can ask whether the Set contains an element equal to bar, so why can't I get that element? :(

                  为了澄清,equals 方法被覆盖,但它只检查其中一个字段,而不是全部.所以两个被认为相等的 Foo 对象实际上可以有不同的值,这就是为什么我不能只使用 foo.

                  To clarify, the equals method is overridden, but it only checks one of the fields, not all. So two Foo objects that are considered equal can actually have different values, that's why I can't just use foo.

                  推荐答案

                  如果元素相等,那么获取元素就没有意义了.Map 更适合这个用例.

                  There would be no point of getting the element if it is equal. A Map is better suited for this usecase.

                  如果你仍然想找到元素,你别无选择,只能使用迭代器:

                  If you still want to find the element you have no other option but to use the iterator:

                  public static void main(String[] args) {
                  
                      Set<Foo> set = new HashSet<Foo>();
                      set.add(new Foo("Hello"));
                  
                      for (Iterator<Foo> it = set.iterator(); it.hasNext(); ) {
                          Foo f = it.next();
                          if (f.equals(new Foo("Hello")))
                              System.out.println("foo found");
                      }
                  }
                  
                  static class Foo {
                      String string;
                      Foo(String string) {
                          this.string = string;
                      }
                      @Override
                      public int hashCode() { 
                          return string.hashCode(); 
                      }
                      @Override
                      public boolean equals(Object obj) {
                          return string.equals(((Foo) obj).string);
                      }
                  }
                  

                  这篇关于从 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='NRKTH'></small><noframes id='NRKTH'>

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

                        1. <tfoot id='NRKTH'></tfoot>
                            <bdo id='NRKTH'></bdo><ul id='NRKTH'></ul>