2012-06-26 6 views
0
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); 
       } 
      } 

이것은 내가 작성한 코드 세그먼트 (Stanford CoreNLP를 사용함)입니다. 내가 직면 한 문제는 현재이 코드가 하나의 맵, 즉 "단어"로만 작동한다는 것입니다. 이제 필자는 파서가 내 구분 기호 인 "000000000"을보고하자 마자 목록에 새 맵을 추가 한 다음 키와 값을 삽입해야합니다. "000000000"이 표시되지 않으면 키와 값이 동일한 맵에 추가됩니다. 많은 노력을 기울여도 할 수 없으므로 도와주세요.HashMaps 목록 Java

+0

당신이 예를 들어 줄 수

는 또한 (나는 그것을 컴파일 시도하지 않은) 여기 버전을 수정, 당신은 당신의 코드를 다른 문제가 보이지? –

답변

2

나는

그래서 예를 들어 currentMapwords의 이름을 변경하고 추가 ... listOfHash의 모든지도를 포함 할 것 같다. 당신이 볼 때 "000000000" currentMap에 할당, 새로운 맵의 인스턴스를리스트에 추가하고 계속 ...

뭔가 같은 :

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 to listOfHash.

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

} 
+0

thanx 많이 pgras ..하지만 실제로 "당신의 초기지도를 listOfHash에 추가하는 것을 잊지 마라"는 것은 무엇을 의미합니까? 조금 설명해 주시겠습니까? – agarwav

+0

나는 더 완전한 응답을주었습니다 ... – pgras

+0

"currentMap"right coz u로 "words"를 대체해야합니다. 여기에 그것을하지 않았습니다. 고맙습니다. 많이 고맙습니다. – agarwav