【问题标题】:How to make the value of a map blank while comparing it with the values of another map?如何在将地图的值与另一张地图的值进行比较时将其设为空白?
【发布时间】:2019-02-27 21:17:04
【问题描述】:

假设我有两张地图 region1、region2 并且都被声明为

LinkedHashMap<String, List<String>> region1 = new LinkedHashMap<>();

LinkedHashMap<String, List<String>> region2 = new LinkedHashMap<>();

region1 包含以下值:

region1 : {R1 = [A, B, C, D]}

region2 包含以下值:

region2 : {R2 = [G, A, D, B]}

将region1的值作为基值,遍历region2的值,不重复的值必须放在另一个map中,key为region2,不重复的值为region2。

所以地图 region3 包含

region3 : {R2 = [G]}

我们在另一个地图上迭代 region3 以比较匹配的值

map1 : {1 = [G, C]}

由于值中有匹配,我们需要在匹配的值中添加一个空格。

所以 map1 包含值

map1 : {1=[  , C]}

【问题讨论】:

  • map3 = new LinkedHashMap&lt;&gt;(region2); map3.removeAll(region1.keySet());?使用上面的值,您将在map3 中得到G
  • Considering the values of region1 as base values - 您是指 R1 键还是与 R1 关联的列表中的值?我认为显示您期望输出的样子会很有帮助。
  • @Jason 列表中与 R1 关联的值。
  • 我不明白,地图有不同的键。我们是否必须迭代所有可能的条目?
  • @LppEdd 是的,我们需要迭代所有可能的条目

标签: java list hashmap linkedhashmap


【解决方案1】:

编辑:优化。
仅限Stream。原始对象没有被修改。
不过有点长。

final Map<String, List<String>> region1 = new LinkedHashMap<>();
region1.put("R1", new ArrayList<>(Arrays.asList("A", "B", "C", "D")));

final Map<String, List<String>> region2 = new LinkedHashMap<>();
region2.put("R2", new ArrayList<>(Arrays.asList("A", "G", "C", "B")));
region2.put("R3", new ArrayList<>(Arrays.asList("A", "G", "C", "B")));

final Stream<Entry<String, List<String>>> entries =
        region1.values()
               .stream()
               .flatMap(values ->
                       region2.entrySet()
                              .stream()
                              .map(e -> {
                                  final List<String> value =
                                          e.getValue()
                                           .stream()
                                           .filter(v -> !values.contains(v))
                                           .collect(toList());
                                  return new SimpleEntry<>(e.getKey(), value);
                              })
               );

final Map<String, List<String>> result = entries.collect(toMap(Entry::getKey, Entry::getValue));

这就是我喜欢Streams 的原因。使用 类型推断 (Java 10+) 会更加简洁。

【讨论】:

    【解决方案2】:

    不使用流,可以这样解决:

    public static void main(String[] args) {
    
        // setup the input and output maps
        LinkedHashMap<String, List<String>> region1 = new LinkedHashMap<>();
        LinkedHashMap<String, List<String>> region2 = new LinkedHashMap<>();
        LinkedHashMap<String, List<String>> region3 = new LinkedHashMap<>();
        region1.put("R1", asList("A", "B", "C", "D"));
        region2.put("R2", asList("G", "A", "D", "B"));
    
        // for each key in region2
        for(String key : region2.keySet()) {
    
            // make a copy of the values for that key
            List<String> values = new ArrayList<>(region2.get(key));
    
            // remove all the duplicates from each of the lists in region1
            for (List<String> baseValues : region1.values()) {
                values.removeAll(baseValues);
            }
    
            // if there are any remaining list values
            if (values.size() > 0) {
                // put them into region3
                region3.put(key, values);
            }
        }
    
        System.out.println(region3);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-25
      • 2011-02-10
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多