2017-04-05 75 views
-4

我有一個HashMap包含單詞Map<String,ArrayList<string>> 的列表,也包含話,我想比較這個名單與HashMap這給了我們另一個HashMap包含列表二進制值 1如果單詞存在0,如果不存在。 The problem is it adds the first list to the 2nd .... 代碼比較HashMap的<字符串,ArrayList的<String>>和ArrayList <String>的Java

HashMap<String,ArrayList<String>> hmm=new       
HashMap<String,ArrayList<String>>(); 
System.out.println("affichage liste:"); 
for (Entry<String, List<String>> ee : hm.entrySet()) { 
String key = ee.getKey(); 
List<String> values = ee.getValue(); 
for (String temp : list) 
    hmm.add(values.contains(temp) ? "1" : "0"); 
} 
+2

請將您的代碼以文本形式發佈,而不是圖片形式。 – px06

+0

請參閱下面的答案。 –

+0

我不明白比較'地圖<字符串,列表>'到'列表'背後的過程 - 他們自己顯然永遠不會彼此相等。那是地圖鍵?什麼是地圖值?列表中有什麼?什麼是成功的比較?什麼不是?請詳細說明你的問題,這對我來說目前沒有任何意義。 –

回答

0

你可以比較像下面的代碼:

List<String> global = new ArrayList<String>; 
    Map<String, ArrayList<String>> newMap = new HashMap<String, ArrayList<String>>(); 
    Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); 
    for (String key:map.keySet()) { 
     List<String> arrayList= map.get(key); 
     for (String words:arrayList) { 
      List<String> newList = new ArrayList<String>; 
      for (String globallist:global) { 
       if(words.equals(globallist)){ 
        newList.add(words); 
       } 
      } 
      newMap.put(key,newList); 
     } 
    } 
+0

thnk你但我希望在全局列表中使用hashmap中的值進行比較,並將rsult(o,1) hashmap –

+0

@FzKaddour看到我的更新代碼,它可以幫助你。 –

+0

非常有用非常感謝@Chetan –

0

根據你的幫助,我發現這個解決方案非常感謝 根據你的幫助,我發現這個解決方案非常感謝

   HashMap<String,ArrayList<String>> hmm=new    
      HashMap<String,ArrayList<String>>(); 
      for (Entry<String, List<String>> ee : hm.entrySet()) { 
      String key = ee.getKey(); 

      List<String> values = ee.getValue(); 
      List<String> list5=new ArrayList<String>(); 
      for (String temp : global){ 

      list5.add(values.contains(temp) ? "1" : "0"); 


       } 
       hmm.put(key, (ArrayList<String>) list5); 




      }' 
相關問題