2014-05-16 2 views
0
public static void main(String args[]) throws FileNotFoundException, IOException 
{ 


    FileReader in = new FileReader("/home/aoblah/Downloads/file1.txt"); 
    BufferedReader br = new BufferedReader(in); 



    // Scanner in = null; 
    String answer = null; 
    String verb = null; 
    String line = null; 
    String [] split_npn = null; 
    String [] split_pn = null; 
    String [] split_dp2 = null; 
    String [] split_dp3 = null; 

    HashMap<String, HashMap<String, ArrayList<String>>> hmap = new HashMap<String, HashMap<String, ArrayList<String>>>(); 

    try { 


     while ((line = br.readLine()) != null) 
     { 
      if(line.contains("verb")) 
      { 

       verb = line.substring(0, line.indexOf("(")); 

       if(line.contains("npn")){ 
        String test = line.substring(line.indexOf("npn")); 
        answer = test.substring(test.indexOf("npn"),test.indexof(']')); 

        answer = answer.replace(",", " ");       
        split_npn = answer.split(" "); 
       } 




       if(line.contains("pn")){ 
        String test = line.substring(line.indexOf("pn")); 
        answer = test.substring(test.indexOf("pn"), test.indexOf(']')); 
        answer = answer.replace(",", " ");       
        split_pn = answer.split(" "); 
       } 

       if(line.contains("dp2")){ 
        String test = line.substring(line.indexOf("dp2")); 
        answer = test.substring(test.indexOf("dp2"), test.indexOf(']')); 
        answer = answer.replace(",", " ");       
        split_dp2 = answer.split(" "); 
       } 
       if(line.contains("dp3")){ 
        String test = line.substring(line.indexOf("dp3")); 
        answer = test.substring(test.indexOf("dp3"), test.indexOf(']')); 
        answer = answer.replace(",", " "); 
        split_dp3 = answer.split(" "); 
       }      


      } 

      if(split_npn != null){ 
       ArrayList<String> npn = new ArrayList<String>(); 
       hmap.put(verb, new HashMap<String, ArrayList<String>>()); 
       for(int i = 1; i< split_npn.length; i++){ 
        npn.add(split_npn[i]); 
       } 
       npn.trimToSize(); 
       hmap.get(verb).put("npn", npn); 
      } 

      if(split_pn != null){ 
       ArrayList<String> pn = new ArrayList<String>(); 
       hmap.put(verb, new HashMap<String, ArrayList<String>>()); 
       for(int i = 1; i< split_pn.length; i++){ 
        pn.add(split_pn[i]); 
       } 
       pn.trimToSize(); 
       hmap.get(verb).put("pn", pn); 
      }     

      if(split_dp2 != null){ 
       ArrayList<String> dp2 = new ArrayList<String>(); 
       hmap.put(verb, new HashMap<String, ArrayList<String>>()); 
       for(int i = 1; i< split_dp2.length; i++){ 
        dp2.add(split_dp2[i]); 
       } 
       dp2.trimToSize(); 
       hmap.get(verb).put("dp2", dp2); 
      }         

      if(split_dp3 != null){ 
       ArrayList<String> dp3 = new ArrayList<String>(); 
       hmap.put(verb, new HashMap<String, ArrayList<String>>()); 
       for(int i = 1; i< split_dp3.length; i++){ 
        dp3.add(split_dp3[i]); 
       } 
       dp3.trimToSize(); 
       hmap.get(verb).put("dp3", dp3); 
      } 

      System.out.println(hmap); 

     } 

    } 


    catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

파일에서이 항목을 포함 같은 침착 - 자바 힙 공간

(

로 동사, NAUX, [nullobj, [DP2, 아래로], [DP3, 아래로]], 고요한).
평온 (동사, [sg], 평온).
평온한 (동사, [sg, pl, en], 평온).
calm (동사, [pl, inf], calm).
진정 (verb, [ing], calm).
침착하게 (adv, mv, calmly).
평온 (명사, [sg], 평온).

나는 모든 동사, 그것과 관련된 전치사의 카테고리 및 전치사를 중첩 해시 맵에 저장하려고합니다. 동사와 관련된 특정 전치사 범주 만 인쇄 할 때 문제가 없지만 전체 해시 맵을 표시하면 프로그램이 실행되고 멈추지 않습니다 ... 공간 힙 오류에서 Java로 끝납니다. 이 메모리 문제를 어떻게 해결할 수 있습니까?

+0

파일의 크기는 얼마나됩니까? – chickenpie

+0

테스트 할 작은 파일로 시도해보십시오. –

+0

이 게시물을 확인하십시오 : http://stackoverflow.com/questions/9619092/java-heap-space-hashmap-arraylist?rq=1 – chickenpie

답변

0

한 가지 제안은 System.out.println (hmap)을 수행하는 대신,

는 sequentually 그것을 밖으로 인쇄 해 봅니다 :

for(String outerKey: hmap.keySet()) 
{ 
    System.out.println(outerKey + ": "); 
    Map inner = hmap.get(outerKey); 
    for(String innerKey : innerMap) 
    { 
    System.out.print(innerKey + ": "); 
    System.out.println(innterMap.get(innerKey).toString()); 
    } 

} 
+0

감사합니다. 나는 그것을 시도 할 것이다. – dirdir69

0

당신은

System.out.println(hmap) 

이 전체 출력을위한 메모리에 하나의 큰 문자열을 구축 HashMaptoString() 방법을 사용하여지도를 인쇄 할 수 있습니다. 입력 파일의 크기에 따라 많은 메모리가 될 수 있습니다.

항목 반복지도를 인쇄하는 것이 좋습니다 것 :

for (Object e : hmap.entrySet()) { 
    System.out.print(e); 
} 
System.out.println(); 

이, 그것을 인쇄, 하나 개의 항목에 대한 메모리를 할당 가비지 컬렉터에 손하고 다음을 취할 것입니다.

+0

아 ... 알았어. 나는 이것을 시험 할 것이다. 감사. – dirdir69

+0

메모리/시간 절약에 도움이되지 않습니다. 프로그램이 아직 실행 중입니다. – dirdir69