【问题标题】:How to get union of TreeMaps with Sets as values?如何获得 TreeMaps 与 Sets 作为值的联合?
【发布时间】:2018-04-24 03:23:50
【问题描述】:

我有几个临时 TreeMap,我想将它们组合成一个 Super TreeMap,即较小 TreeMap 的联合。

我的 TreeMaps 的通用类型是

TreeMap<String, Set<Integer>>

当我尝试打电话时

SuperTreeMap.addALL(temp)

我收到以下错误

Error: cannot find symbol.

【问题讨论】:

    标签: java set treemap set-union


    【解决方案1】:

    您不能将所有Set 合并为putAll 的值。每个新的Set 都将替换现有的Set。你必须做很多事情:

    Map<String, Set<Integer>> map = new TreeMap<>();
    Map<String, Set<Integer>> map1 = new TreeMap<>();
    Map<String, Set<Integer>> map2 = new TreeMap<>();
    
    map1.put("one", new HashSet<>());
    map1.get("one").add(1);
    map1.get("one").add(2);
    map1.get("one").add(3);
    
    map1.put("two", new HashSet<>());
    map1.get("two").add(1);
    map1.get("two").add(2);
    map1.get("two").add(3);
    
    map2.put("one", new HashSet<>());
    map2.get("one").add(4);
    map2.get("one").add(5);
    map2.get("one").add(6);
    
    map2.put("two", new HashSet<>());
    map2.get("two").add(4);
    map2.get("two").add(5);
    map2.get("two").add(6);
    
    for (Map<String, Set<Integer>> m : Arrays.asList(map1, map2)) {
        for (Map.Entry<String, Set<Integer>> entry : m.entrySet()) {
            if (!map.containsKey(entry.getKey()))
                map.put(entry.getKey(), new HashSet<>());
            map.get(entry.getKey()).addAll(entry.getValue());
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以创建一个自定义addALL 方法来组合两个地图的条目,并在其中包含两个集合的元素:

      /**
       * Combines the entries of several maps into the {@link TreeMap}
       * and the elements of sets inside them into the {@link TreeSet}.
       *
       * @param maps the collection of maps to be combined
       * @param <T>  the type of keys of maps
       * @param <U>  the type of elements of sets
       * @return combined map of sets
       */
      public static <T, U> Map<T, Set<U>> addAll(Collection<Map<T, Set<U>>> maps) {
          return maps.stream()
                  // Stream<Map.Entry<String,Set<Integer>>>
                  .flatMap(map -> map.entrySet().stream())
                  // stream of map entries into one map
                  .collect(Collectors.toMap(
                          // key is the same
                          Map.Entry::getKey,
                          // value is the same
                          Map.Entry::getValue,
                          // combining elements of two collections
                          (set1, set2) -> Stream.concat(set1.stream(), set2.stream())
                                  // collectionFactory
                                  .collect(Collectors.toCollection(TreeSet::new)),
                          // mapFactory
                          TreeMap::new));
      }
      
      // test
      public static void main(String[] args) {
          Map<String, Set<Integer>>
                  map1 = Map.of("one", Set.of(1, 2, 3), "two", Set.of(1, 2, 3)),
                  map2 = Map.of("one", Set.of(4, 5, 6), "two", Set.of(4, 5, 6)),
                  // combine the entries of two maps into one map
                  map3 = addAll(Set.of(map1, map2));
          // output
          System.out.println(map3);
          // {one=[1, 2, 3, 4, 5, 6], two=[1, 2, 3, 4, 5, 6]}
      }
      

      另见:How do I take the union of sets?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        • 2015-07-19
        • 2013-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        相关资源
        最近更新 更多