簡體   English   中英

如何保存地圖 <String,list<String> &gt;通過自檢其鍵值的值來訪問另一個相同類型的映射

[英]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>> 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);
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM