繁体   English   中英

生成包含两个地图中包含的值的交集/联合的地图<X,Set<Y> &gt;

[英]Generate an Map containing an Intersection/Union of values contained in two maps Map<X,Set<Y>>

我有两个地图参数: Map<X,Set<Y>> map1Map<X,Set<Y>> map2

我正在寻找一种方法来编写一种方法,该方法生成一个新映射,其中包含两个映射中都存在的交集,以及map1map2中存在的联合

换句话说,对于任何X x如果它在map1的域中但不在map2的域中,那么它的值将是map1.get(x) 在相反的情况下也一样。 如果它在它们两个中,那么我想返回一个集合,它是map1.get(x)map2.get(x)的交集。

假设我知道X是哪个类,这可以通过以下代码完成:

public Map<X,Set<Y>> unifyAndIntersect(Map<X,Set<Y>> map1, Map<X,Set<Y>> map2) {
    Map<X,Set<Y>> combined = new HashMap();
    combined.putAll(map1);
    for(X x : map2.keySet()){
          if(combined.contains(x)){
                Set<Y> res = Sets.newSet();
                res.addAll(map1.get(x));
                res.retainAll(map2.get(x));
                combined.put(x,res);
          }
          else{
                combined.put(x,map2.get(x));
          }
    }
}

但是,我想让这个方法通用,从某种意义上说它适用于任何XY 我曾尝试使用Object ,但从我的类类型转换为Object时出错...

你能告诉我什么是正确的方法吗?

为了声明一个泛型方法,您需要在方法修饰符和返回类型之间提供泛型类型参数<X,Y>有关更多信息,请参阅)。 没有映射类型Map<X,Set<Y>>中的XY将不会被视为类型的通用“占位符”,但作为类型本身和编译器会抱怨类型XY是未知的。

不要忘记右侧的菱形<> ,同时实例化一个泛型类, new HashMap(); 没有菱形会创建一个类型的地图。

您提供的代码中也存在不一致:如果两个映射都存在一个键,则会将一个新集合作为值添加到结果映射中,但是如果根据您的代码键仅包含在其中一个中将使用现有的集合。 我最好通过为每个值生成一个新集合来确保对结果映射值的后续修改不会影响map1map2的状态。

public <X,Y> Map<X, Set<Y>> unifyAndIntersect(Map<X,Set<Y>> map1,
                                              Map<X,Set<Y>> map2) {
    
    Map<X, Set<Y>> combined = new HashMap<>();
    for(Map.Entry<X, Set<Y>> entry: map1.entrySet()){
        Set<Y> union = new HashSet<>(entry.getValue());
        if (map2.containsKey(entry.getKey())) {
            union.retainAll(map2.get(entry.getKey()));
        }
        combined.put(entry.getKey(), union);
    }
    return combined;
}

使用 Stream API 可以实现相同的逻辑:

public <X,Y> Map<X, Set<Y>> unifyAndIntersect(Map<X,Set<Y>> map1,
                                              Map<X,Set<Y>> map2) {
    return map1.entrySet().stream()
        .map(entry -> {
            Set<Y> set2 = map2.get(entry.getKey());
            return set2 == null ? Map.entry(entry.getKey(), new HashSet(entry.getValue())) : 
                Map.entry(entry.getKey(),
                entry.getValue().stream()
                    .filter(set2::contains)
                    .collect(Collectors.toSet()));
        })
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM