【问题标题】:How to save map<String,list<String>> to another map of same type by self checking the values for its key's value如何通过自我检查其键值的值将 map<String,list<String>> 保存到另一个相同类型的地图
【发布时间】:2018-04-17 09:18:24
【问题描述】:

我有一个“字符串作为键和字符串列表作为值”的映射。我想检查键值对并希望地图自行查找其新值并制作所有组合的完整路径。参考这张图片

我希望这张图片能给出一个清晰的想法,因为我无法用语言来解释这一点。请帮我解决这个问题。提前谢谢你。

【问题讨论】:

    标签: javascript salesforce apex-code apex


    【解决方案1】:

    快速而肮脏的解决方案:

    Map<String, List<String>> sndMap = new Map<String, List<String>>();
    // Go throuch first map and copy all keys to second map
    for (String key : firstMap.keySet()) {
      if (!sndMap.containsKey(key)) {
        sndMap.put(key, new List<String>());
      }
     List<String> tmp = sndMap.get(key);
     tmp.add(firstMap.get(key).get(0));
     sndMap.put(key, tmp);
     } 
     // Go throuch all List of values in first map and check if a value is a key in the second map.  
     for (List<String> values : firstMap.values()) {
       for (String s : values) {
        if (sndMap.containsKey(s)) {
            List<String> tmpList = sndMap.get(s);
            tmpList.add(s);
            sndMap.put(s, tmpList);
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2020-01-27
      相关资源
      最近更新 更多