2017-04-04 43 views
3

我正在檢查地圖是否包含其他地圖的所有內容。 例如,我有一個地圖,其是一個Map<String, List<String>>,所述元素是: 「1」 - > [ 「A」, 「B」] 「2」 - > [ 「C」, 「d」]檢查地圖是否包含其他地圖的所有內容

另一個MapB這也是一個Map<String, List<String>>,所述元素是: 「1」 - > [ 「A」] 「2」 - > [ 「C」, 「d」],

我想創建一個功能比較(mapA,mapB)在這種情況下將返回false。

這樣做的最好方法是什麼?

感謝

+2

只爲你的未來的自己的理智,不叫它比較。你沒有比較。稱之爲'containsAll'或'subsumes'。 –

回答

4

裏面你compare(mapA, mapB)方法,你可以簡單地使用:由@Jacob摹提供

0

回答你的情況不會工作,將工作只有額外的(鍵,值)對在MapA中。像MapA = {1「 - > [」a「,」b「]」2「 - > [」c「,」d「]}和MapB = {1」 - > [「a」,「b」] }。

什麼ü需要的是這個 -

boolean isStrictlyDominate(LinkedHashMap<Integer, HashSet<Integer>> firstMap, LinkedHashMap<Integer, HashSet<Integer>> secondMap){ 
    for (Map.Entry<Integer, HashSet<Integer>> item : secondMap.entrySet()) { 
     int secondMapKey = item.getKey(); 
     if(firstMap.containsKey(secondMapKey)) { 
      HashSet<Integer> secondMapValue = item.getValue(); 
      HashSet<Integer> firstMapValue = firstMap.get(secondMapKey) ; 
      if(!firstMapValue.containsAll(secondMapValue)) { 
       return false; 
      } 

     } 
    } 
    return !firstMap.equals(secondMap); 
} 

(如果你不想檢查嚴格統治那麼就return最後return說法正確)

相關問題