1. <tfoot id='bcdNI'></tfoot>
      • <bdo id='bcdNI'></bdo><ul id='bcdNI'></ul>
    2. <legend id='bcdNI'><style id='bcdNI'><dir id='bcdNI'><q id='bcdNI'></q></dir></style></legend>

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

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

        在 Java 中比较两组的最快方法是什么?

        What is the fastest way to compare two sets in Java?(在 Java 中比较两组的最快方法是什么?)
          <bdo id='YeIGN'></bdo><ul id='YeIGN'></ul>

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

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

                  本文介绍了在 Java 中比较两组的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试优化一段比较列表元素的代码.

                  I am trying to optimize a piece of code which compares elements of list.

                  例如.

                  public void compare(Set<Record> firstSet, Set<Record> secondSet){
                      for(Record firstRecord : firstSet){
                          for(Record secondRecord : secondSet){
                              // comparing logic
                          }
                      }
                  }
                  

                  请注意集合中的记录数会很高.

                  Please take into account that the number of records in sets will be high.

                  谢谢

                  谢卡尔

                  推荐答案

                  firstSet.equals(secondSet)
                  

                  这真的取决于你想在比较逻辑中做什么......即如果你在一个集合中找到一个元素而不在另一个集合中会发生什么?你的方法有一个 void 返回类型,所以我假设你会在这个方法中做必要的工作.

                  It really depends on what you want to do in the comparison logic... ie what happens if you find an element in one set not in the other? Your method has a void return type so I assume you'll do the necessary work in this method.

                  如果需要,可以进行更细粒度的控制:

                  More fine-grained control if you need it:

                  if (!firstSet.containsAll(secondSet)) {
                    // do something if needs be
                  }
                  if (!secondSet.containsAll(firstSet)) {
                    // do something if needs be
                  }
                  

                  如果您需要获取一组中的元素而不是另一组中的元素.
                  set.removeAll(otherSet) 返回一个布尔值,而不是一个集合.要使用 removeAll(),您必须复制该集合然后使用它.

                  If you need to get the elements that are in one set and not the other.
                  set.removeAll(otherSet) returns a boolean, not a set. To use removeAll(), you'll have to copy the set then use it.

                  Set one = new HashSet<>(firstSet);
                  Set two = new HashSet<>(secondSet);
                  one.removeAll(secondSet);
                  two.removeAll(firstSet);
                  

                  如果 onetwo 的内容都是空的,那么你知道这两个集合是相等的.如果不是,那么你已经得到了使集合不相等的元素.

                  If the contents of one and two are both empty, then you know that the two sets were equal. If not, then you've got the elements that made the sets unequal.

                  您提到记录的数量可能很高.如果底层实现是一个HashSet,那么每条记录的获取都是在O(1) 时间内完成的,所以没有比这更好的了.TreeSetO(log n).

                  You mentioned that the number of records might be high. If the underlying implementation is a HashSet then the fetching of each record is done in O(1) time, so you can't really get much better than that. TreeSet is O(log n).

                  这篇关于在 Java 中比较两组的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的默认语言环境设置以使其保持一致?)
                    • <bdo id='w2R2A'></bdo><ul id='w2R2A'></ul>
                      • <small id='w2R2A'></small><noframes id='w2R2A'>

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

                        <tfoot id='w2R2A'></tfoot>

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