Java哈希映射列表 Map words=newhashmap(); List listOfHash=新的ArrayList(); 对于(int-temp=0;temp

Java哈希映射列表 Map words=newhashmap(); List listOfHash=新的ArrayList(); 对于(int-temp=0;temp,java,list,hashmap,stanford-nlp,Java,List,Hashmap,Stanford Nlp,这是我编写的一段代码(它使用斯坦福CoreNLP)。我面临的问题是,目前这段代码只处理一个映射,即“单词”。现在,我希望一旦解析器看到“000000000”,这是我的分隔符,那么应该向列表中添加一个新的映射,然后向其中插入键和值。如果未看到“000000000”,则将在同一映射中添加键和值。 请帮助我,因为即使我付出了很多努力,我也无法做到。我想灰烬列表将包含您所有的地图 因此,将words重命名为currentMap,并添加到其中。当您看到“000000000”实例化一个新映射时,将其分配给c

这是我编写的一段代码(它使用斯坦福CoreNLP)。我面临的问题是,目前这段代码只处理一个映射,即“单词”。现在,我希望一旦解析器看到“000000000”,这是我的分隔符,那么应该向列表中添加一个新的映射,然后向其中插入键和值。如果未看到“000000000”,则将在同一映射中添加键和值。
请帮助我,因为即使我付出了很多努力,我也无法做到。

我想灰烬列表将包含您所有的地图

因此,将
words
重命名为
currentMap
,并添加到其中。当您看到“000000000”实例化一个新映射时,将其分配给
currentMap
,将其添加到列表中并继续

比如:

Map<String, List<String>> words = new HashMap<String, List<String>>();
            List<Map> listOfHash = new ArrayList<Map>();

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    String word = getTagValue("word", eElement);
                    List<String> add_word = new ArrayList<String>();
                    String pos = getTagValue("POS", eElement);
                    if(words.get(pos)!=null){
                        add_word.addAll(words.get(pos));
                        add_word.add(word);
                    }
                    else{
                        add_word.add(word);
                    }
                    words.put(pos, add_word);
                }
            }
if(“000000000”。等于(字)){
currentMap=newhashmap();
添加列表(当前映射);
继续;//如果我们不想跳过“000000000”的插入
}
别忘了把你的初始地图添加到Hash列表中

我还发现您的代码还有其他问题,这里是修改后的版本(我没有尝试编译):

Map currentMap=newhashmap();
List listOfHash=新的ArrayList();
添加列表(当前映射);
对于(int-temp=0;temp
你能举个例子吗?有很多。。但你们所说的“别忘了将你们的初始地图添加到灰烬列表”到底是什么意思呢。你能解释一下吗?我需要用“currentMap”替换“words”,因为你还没有在这里这么做。还有很多
if ("000000000".equals(word)){
    currentMap = new HashMap<String, List<String>>();
    listOfHash.add(currentMap);
    continue; // if we wan't to skip the insertion of "000000000"
}
Map<String, List<String>> currentMap = new HashMap<String, List<String>>();
List<Map> listOfHash = new ArrayList<Map>();
listOfHash.add(currentMap);


for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        String word = getTagValue("word", eElement);    

        if ("000000000".equals(word)){
            currentMap = new HashMap<String, List<String>>();
            listOfHash.add(currentMap);
            continue; // if we wan't to skip the insertion of "000000000"
        }

        String pos = getTagValue("POS", eElement);

        List<String> add_word = currentMap.get(pos);
        if(add_word==null){
            add_word = new ArrayList<String>();
            currentMap.put(pos, add_word);
        }
        add_word.add(word);
    }

}