2013-12-25 14 views
1

덤프가 데이터베이스 (이 경우에는 postgres)에 시트를 인쇄하는 유틸리티 코드를 작성하고 있습니다. 나는 poi의 HSSF 기술을 사용하여 Excel 시트를 처리했습니다. Excel의 각 열을 String 유형의 ArrayLists에 별도로 저장합니다. 이 경우 .xls의 열 수가 23보다 큰 경우 eclipse에서 'heap memory full'오류가 발생합니다. 내 질문은 이러한 arraylists 하나의 컬렉션 개체와 어떤 방법을 사용해야 결합 할 수 있습니까?여러 ArrayList를 단일 Collection Java로 대체하는 방법

public ArrayList<String> getList(String path, String srnoStr, 
     String nameStr, String dobStr, String genderStr, String addressStr, 
     String pinStr, String mobStr, String eIdStr, String categoryStr, 
     String branchStr) throws IOException, SQLException { 

    ArrayList<String> errorList = new ArrayList<String>(); 
    ArrayList<String> cellError = null; 

    // String error=null; 
    // OrderedMap errorMap=new LinkedMap(); 
    // errorMap=null; 

    List<Cell> cells_srno = new ArrayList<Cell>(); 
    List<Cell> cells_name = new ArrayList<Cell>(); 
    List<Cell> cells_dob = new ArrayList<Cell>(); 
    List<Cell> cells_gender = new ArrayList<Cell>(); 
    List<Cell> cells_address = new ArrayList<Cell>(); 
    List<Cell> cells_pin = new ArrayList<Cell>(); 
    List<Cell> cells_mob = new ArrayList<Cell>(); 
    List<Cell> cells_eId = new ArrayList<Cell>(); 
    List<Cell> cells_category = new ArrayList<Cell>(); 
    List<Cell> cells_branch = new ArrayList<Cell>(); 

      try { 
     int srnoIndex = 0; 
     srnoIndex = getIndex(srno, path); 
     cellError = fillList(srnoIndex, srNoId, cells_srno, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int nameIndex = 0; 
     nameIndex = getIndex(name, path); 
     cellError = fillList(nameIndex, nameId, cells_name, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int dobIndex = 0; 
     dobIndex = getIndex(dob, path); 
     cellError = fillList(dobIndex, dobId, cells_dob, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int genderIndex = 0; 
     genderIndex = getIndex(gender, path); 
     cellError = fillList(genderIndex, genderId, cells_gender, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int addressIndex = 0; 
     addressIndex = getIndex(address, path); 
     cellError = fillList(addressIndex, addressId, cells_address, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int pinIndex = 0; 
     pinIndex = getIndex(pin, path); 
     cellError = fillList(pinIndex, mobId, cells_pin, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int mobIndex = 0; 
     mobIndex = getIndex(mob, path); 
     cellError = fillList(mobIndex, pinId, cells_mob, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int eIdIndex = 0; 
     eIdIndex = getIndex(eId, path); 
     cellError = fillList(eIdIndex, eIdId, cells_eId, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int categoryIndex = 0; 
     categoryIndex = getIndex(category, path); 
     cellError = fillList(categoryIndex, categoryId, cells_category, 
       path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     cellError = null; 
     int branchIndex = 0; 
     branchIndex = getIndex(branch, path); 
     cellError = fillList(branchIndex, branchId, cells_branch, path); 
     if (cellError != null) 
      errorList.addAll(cellError); 

     // System.out.println(cells); 
     int n = cells_srno.size(); 
     int k = 0; 
     System.out.println("Total number Rows = " + (n - 1)); 
     int j = 0; 
     // System.out.println("Column name=" +cells.get(0)); 
     if (errorList.isEmpty()) { 
      for (int i = 1; i < n; i++) { 
       k = Insert(cells_srno.get(i), cells_name.get(i), 
         cells_dob.get(i), cells_gender.get(i), 
         cells_address.get(i), cells_pin.get(i), 
         cells_mob.get(i), cells_eId.get(i), 
         cells_category.get(i), cells_branch.get(i)); 
       if (k > 0) 
        j++; 

      } 

     } else { 
      System.out.println("Error"); 
     } 

     /* 
     * System.out 
     * .println("Total no. of physical row in you Excel Sheet=" + j); 
     * System.out 
     * .println("Operation Successfull!! kindly check the database!!! " 
     *); 
     */ 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return errorList; 
} 
+1

엑셀 시트에있는 모든 열에 대해 클래스를 만들고 해당 클래스의 목록을 하나 만드는 것이 좋습니다. – eatSleepCode

+0

u는 게터 세터를 의미합니까 ?? –

+0

당신은 'cells_srno, cells_name, cells_dob 등'과 같은 필드를 가진'class'를 생성하고'List '를 만들 수 있습니다. 그래서 하나의 컬렉션 만 존재하게됩니다. – eatSleepCode

답변

0

나는 Map로 가서 키와 같은 enum와 같은 리팩토링 것 :

public List<String> getList(final String path, final Map<CellType, String> data) { 
    final Map<CellType, List<Cell>> cells = new EnumMap<CellType, List<Cell>>(); 
    for (final CellType cellType : CellType.values()) { 
     cells.put(cellType, new ArrayList<Cell>()); 
    } 

    final List<String> errorList = new ArrayList<String>(); 
    try { 
     for (final CellType cellType : data.keySet()) { 
      final int index = getIndex(data.get(cellType), path); 
      final List<String> cellError = fillList(index, /* the ID for cellType */, cells.get(cellType), path); 
      if (cellError != null) { 
       errorList.addAll(cellError); 
      } 
     } 

     // ... (use the map) 
    } catch (final Exception e) { 
     e.printStackTrace(); 
    } 

    return errorList; 
} 

public static enum CellType { 
    SRNO, NAME, DOB // ... 
} 

(테스트하지)

NB

가 : srNoId, nameId 등 코드에 정의되지 않은 당신이 게시했기 때문에 나는 그들이 무엇인지 모른다.

+0

HashMap이 상황을 처리하는 가장 좋은 기술 이었지만 setter 및 getter 메서드를 사용하기로 결정했습니다. & 마침내 내가 찾던 것을 달성했습니다 .... 귀중한 응답에 감사드립니다 ....! –

1

메모리 부족이 문제가된다면 나는 당신의 접근법이 매우 g라고 생각하지 않는다. 대다수. 여러 컬렉션 대신 하나의 큰 컬렉션을 갖는 대신 스트리밍을 통해 메모리 사용량을 줄여야합니다. 그런 식으로는 거의 저장하지 않을 것이며 입력 데이터가 조금 더 커지면 같은 문제가 다시 발생합니다.

e.e. 목록을 미리 채우기보다는 한 번에 한 행을 처리 (삽입)하십시오. 정상적인 라이브러리를 사용하여 스트리밍되는 입력 엑셀 시트를 읽는 경우 디스크에 들어갈 수있는 한 많은 양의 데이터를 처리 할 수 ​​있어야합니다.