Java中的地图和列表 NodeList nList2=doc.getElementsByTagName(“dep”); Map depMap=newhashmap(); 对于(int-temp=0;temp

Java中的地图和列表 NodeList nList2=doc.getElementsByTagName(“dep”); Map depMap=newhashmap(); 对于(int-temp=0;temp,java,list,maps,Java,List,Maps,我编写了这段代码,但问题是每当循环运行时,它都会替换已存储在depMap中的列表。我希望它应该将新映射附加到从depMap检索到的列表中,而不是替换它。因此,您需要从映射中获取列表并向该列表添加()项,而不是在每次迭代中创建新列表 改变 NodeList nList2 = doc.getElementsByTagName("dep"); Map<String, List<Map<String, String>>> depMap = new HashMap<

我编写了这段代码,但问题是每当循环运行时,它都会替换已存储在depMap中的列表。我希望它应该将新映射附加到从depMap检索到的列表中,而不是替换它。

因此,您需要从映射中获取列表并向该列表添加()项,而不是在每次迭代中创建新列表

改变

NodeList nList2 = doc.getElementsByTagName("dep");
Map<String, List<Map<String, String>>> depMap = new HashMap<String, List<Map<String, String>>>();
for (int temp = 0; temp < nList2.getLength(); temp++) {
  Element el = (Element)nList2.item(temp);
  String type=el.getAttribute("type");
  Node nNode = nList2.item(temp);
  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    Element eElement = (Element) nNode;
    List<Map<String,String>> depList = new ArrayList<Map<String,String>>();
    String governor = getTagValue("governor", eElement);
    String dependent = getTagValue("dependent", eElement);
    Map<String, String> govdepmap = new HashMap<String, String>();
    govdepmap.put(governor, dependent);
    depList.add(govdepmap);
    List<Map<String,String>> flist = new ArrayList<Map<String,String>>();
    flist.add(govdepmap);
    depMap.put(type, flist);
  }
}
List flist=new ArrayList();

List flist=depMap.get(类型);
if(flist==null){
List flist=new ArrayList();
}

因此,您不需要在每次迭代中创建新列表,而需要从映射中获取列表,并将项目添加到该列表中

改变

NodeList nList2 = doc.getElementsByTagName("dep");
Map<String, List<Map<String, String>>> depMap = new HashMap<String, List<Map<String, String>>>();
for (int temp = 0; temp < nList2.getLength(); temp++) {
  Element el = (Element)nList2.item(temp);
  String type=el.getAttribute("type");
  Node nNode = nList2.item(temp);
  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    Element eElement = (Element) nNode;
    List<Map<String,String>> depList = new ArrayList<Map<String,String>>();
    String governor = getTagValue("governor", eElement);
    String dependent = getTagValue("dependent", eElement);
    Map<String, String> govdepmap = new HashMap<String, String>();
    govdepmap.put(governor, dependent);
    depList.add(govdepmap);
    List<Map<String,String>> flist = new ArrayList<Map<String,String>>();
    flist.add(govdepmap);
    depMap.put(type, flist);
  }
}
List flist=new ArrayList();

List flist=depMap.get(类型);
if(flist==null){
List flist=new ArrayList();
}

flist
govdepmap
depList
的初始化移到for循环之外。

flist
govdepmap
depList
的初始化移到for循环之外。

只需将代码更改为以下代码即可

List<Map<String,String>> flist = depMap.get(type);
if(flist == null){
 List<Map<String,String>> flist = new ArrayList<Map<String,String>>();
}
NodeList nList2=doc.getElementsByTagName(“dep”);
Map depMap=newhashmap();
列表flist=null;
对于(int-temp=0;temp
只需将代码更改为以下代码即可

List<Map<String,String>> flist = depMap.get(type);
if(flist == null){
 List<Map<String,String>> flist = new ArrayList<Map<String,String>>();
}
NodeList nList2=doc.getElementsByTagName(“dep”);
Map depMap=newhashmap();
列表flist=null;
对于(int-temp=0;temp
这个代码很难理解。我建议重构它。我首先将列表移动到一个类中,并公开所需的必要功能。一旦这样做了,你就可以开始分析出哪里出了问题。这个代码很难理解。我建议重构它。我首先将列表移动到一个类中,并公开所需的必要功能。如果是这样做的,你就可以开始破坏正在发生的错误,你可以开始破坏正在发生的错误,你可以开始破坏正在发生的错误,你可以开始破坏正在发生的错误,一旦一旦一旦这样做了,你就可以开始破坏正在发生的错误,你可以开始破坏正在发生的错误,你可以开始破坏正在发生的错误,你可以开始开始破坏正在发生的错误。以下[[[[[[[[[Gon=n=不},,{{{{{{{{{{Gon=n=not}},,},,,,{{{{{{{{{{{{{{{{{{{{n=n=n=n=n},},},},},},},},},{,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}],aux=[{go=am},{buy=to}、{buy=am}、{going=am}、{buy=to}、{buy=am}、{buy=to}、{buy=am}、{buy=car}、{buy=pencil}、{buy=car}、{buy=pencil}、{buy=car}、{buy=pencil}、{buy=pencil}、{buy=car buy}、{buy=pencil}、{buy=buy}、{buy=pencil}、{buy=buy=buy}、{buy=buy}但是{给出了这个输出,但这是错误的,因为他们附加了很多次。嘿,Jigar,这段代码是对的,但是你看到了这里的问题,它对每个列表执行三次任务。它对每个列表执行三次任务。请详细说明,请看{going=not}在neg中被附加了三次,在aux{Gong=am},{buy=to中,{buying=am}重复了三次。如果只追加一次,那么它将是完美的,因为您已经获取了一个列表,该列表可以有多个相同键值的条目{neg=[{going=not},{going=not},{going=not},{comp=[{car a},{pencil=a},{pencil=a},{car car a=a},{pencil=a},{cancil=a},{conj={going=buying},xcomp gong=buy},购买[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[购买]购买]购买]购买]购买]购买]购买]购买]购买]购买]购买]购买]购买[[[[[[[[[[[[[[[[[[[[[[购买]购买]购买]购买]购买]购买]购买]购买]购买]购买]购买]购买]购买[[[[[[[[[[[[[购买]购买]购买]购买]购买]购买]购买]购买]购买]购买[[[[[[[[[[[[[购买]购买]购买]购买]购买]购买]购买[[[[[[[[[[[[[[[_but=[{going=buying},{going=buying}],cc=[{going=but}]}给出了这个输出,但这是错误的,因为它们附加了很多次。嘿,Jigar,这段代码是对的,但你看到了这里的问题,它对每个列表执行三次任务。它对每个列表执行三次任务。请仔细解释一下,请看{going=not}在neg中附加了三次,在aux{going=am},{buy=to},{buy=am}被重复三次。如果这只追加一次,那么它将被执行