【问题标题】:compare HashMap<String,ArrayList<String>> and ArrayList<String> java比较 HashMap<String,ArrayList<String>> 和 ArrayList<String> java
【发布时间】:2017-08-30 22:42:30
【问题描述】:

我有一个HashMap,其中包含一个单词列表Map&lt;String,ArrayList&lt;string&gt;&gt; 还有一个包含单词的列表,我想将此列表与HashMap进行比较,这给了我们另一个包含二进制值的HashMap 1 如果单词存在 0 如果不存在。 The problem is it adds the first list to the 2nd .... 代码是

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");
}

【问题讨论】:

  • 请以文本形式而不是图像形式发布您的代码。
  • 在下面查看我的答案。
  • 我不明白将Map&lt;String, List&lt;String&gt;&gt;List&lt;String&gt; 进行比较背后的过程——就其本身而言,它们显然永远不会彼此相等。那是地图键?什么是地图值?列表里面有什么?什么是成功比较?什么不是?请详细说明您的问题,以目前的形式对我来说没有任何意义。
  • @M.Prokhrov 这里是一个例子,全局列表["a","b","c"] hashmap[1]["a","c"] [2][" a","b"] [3]["a","b","c"] 我想要一个包含 newhashmap[1]["1","0","1"] [2 ]["1","1","0"] [3]["1","1","1"]

标签: java jsp servlets netbeans hashmap


【解决方案1】:

您可以像下面的代码进行比较:

  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);
        }
    }

【讨论】:

  • 谢谢你,但我想将全局列表中的值与 hashmap 中的值进行比较,并将 rsult (o,1) 放入新的 hashmap 中
【解决方案2】:

根据您的帮助,我找到了这个解决方案,非常感谢 根据您的帮助,我找到了这个解决方案,非常感谢

             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);




             }'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多