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

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

        <tfoot id='rRorM'></tfoot>

        我如何接受集合的并集?

        How do I take the union of sets?(我如何接受集合的并集?)

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

              <small id='3Kyug'></small><noframes id='3Kyug'>

                <tbody id='3Kyug'></tbody>

              • <legend id='3Kyug'><style id='3Kyug'><dir id='3Kyug'><q id='3Kyug'></q></dir></style></legend>
                  <bdo id='3Kyug'></bdo><ul id='3Kyug'></ul>
                  本文介绍了我如何接受集合的并集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否有任何最佳方法来获取 n 集的所有并集?

                  Is there any optimal way to get all union of n sets?

                  这是我做过的,但是对于大量的集合来说很慢:

                  This is what I have done, but it is very slow for a large number of sets:

                  public static void main(String[] args) {
                      List<List<Set<Integer>>> unionSet = new ArrayList<>();
                      List<List<Integer>> sets = ...
                      double avail = 0;
                      for (int i = 1; i <= sets.size(); i++) {
                          List<Set<Integer>> us = new ArrayList<>();
                          union(sets, us, new HashSet<>(), i, 0);
                          unionSet.add(us);
                      }
                  }
                  

                  public static void union(
                          List<List<Integer>> sets, List<Set<Integer>> unionSet,
                          Set<Integer> set, int size, int index) {
                      for (int i = index; i < sets.size(); i++) {
                          Set temp = new HashSet(set);
                          temp.addAll(sets.get(i));
                  
                          if (size != 1)
                              union(sets, unionSet, temp, size - 1, i + 1);
                          else
                              unionSet.add(temp);
                      }
                  }
                  

                  n的所有组合的交集

                  推荐答案

                  你可以使用Stream#flatMap方法如下:

                  You can use Stream#flatMap method as follows:

                  1. 如果你有一个setslist,你可以flatten它的元素(即sets) 到一个 set 的唯一值中:

                  1. If you have a list of sets, you can flatten its elements (i.e. sets) into one set of unique values:

                  List<Set<Integer>> setList =
                          List.of(Set.of(1, 2, 3), Set.of(2, 3, 7));
                  
                  Set<Integer> set = setList.stream()
                          .flatMap(Set::stream)
                          .collect(Collectors.toSet());
                  
                  System.out.println(set); // [1, 2, 3, 7]
                  

                • 如果你有一个 deeper 级别的嵌套,那么你必须执行一个 deeper flattening:

                • If you have a deeper level of nesting, then you have to perform a deeper flattening:

                  List<List<Set<Integer>>> lists = List.of(
                          List.of(Set.of(1, 2, 3), Set.of(2, 3, 4)),
                          List.of(Set.of(3, 4, 5), Set.of(5, 1, 2)));
                  
                  Set<Integer> set = lists
                          // Stream<List<Set<Integer>>>
                          .stream()
                          // Stream<Set<Integer>>
                          .flatMap(List::stream)
                          // Stream<Integer>
                          .flatMap(Set::stream)
                          .collect(Collectors.toSet());
                  
                  System.out.println(set); // [1, 2, 3, 4, 5]
                  

                • 如果您有 几个集合,其中 unknown 级别的嵌套,您可以创建一个通用递归展平 方法:

                • If you have several collections with unknown level of nesting, you can create a generic recursive flattening method:

                  public static void main(String[] args) {
                      List<Set<Integer>> setList =
                              List.of(Set.of(1, 2, 3), Set.of(2, 3, 7));
                  
                      List<List<Set<Integer>>> lists = List.of(
                              List.of(Set.of(1, 2, 3), Set.of(2, 3, 4)),
                              List.of(Set.of(3, 4, 5), Set.of(5, 1, 2)));
                  
                      Set<Integer> set = (Set<Integer>) toSet(setList, lists);
                      System.out.println(set); // [1, 2, 3, 4, 5, 7]
                  }
                  

                  public static Set<?> toSet(Collection<?>... collections) {
                      return Arrays.stream(collections)
                              .flatMap(col -> flattenStream(col.stream()))
                              .collect(Collectors.toSet());
                  }
                  

                  public static Stream<?> flattenStream(Stream<?> stream) {
                      return stream.flatMap(e -> {
                          if (e instanceof Collection) {
                              return flattenStream(((Collection<?>) e).stream());
                          } else {
                              return Stream.of(e);
                          }
                      });
                  }
                  


                • 另见:
                  并行矩阵乘法
                  n 集的所有组合的交集

                  这篇关于我如何接受集合的并集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的默认语言环境设置以使其保持一致?)

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

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

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