2017-08-10 25 views
0

Javaストリームストリームを使用して約500Mbの.csvファイルを読み込みました。ほとんどのデータが同じフォーマットですが、2つのインスタンスが見つかりました。 ArrayListに格納されているオブジェクトごとに52行があり、それらをHashMapに追加するので、キーに基づいてアクセスできます。私はHashMapを使って、別のクラスを使って各オブジェクトのExcelファイルを作成し、ファイルが作成されるとすぐにリストを消去し、別のオブジェクトに移動します。問題は、数値が少ない行になるときです。Excelの作成クラスは、NullPointerExceptionをスローする存在しないインデックスから数値を取得しようとします。 NullPointerExceptionがスローされた場合、これらの行をスキップする方法はありますか?私はこの問題が発生した場合、私は52行をスキップしなければならないことを知っています。例外がスローされた場合、csvの行をスキップします。Javaストリーム

try 
    { 
     final String regex = "\\d*\\.?\\d+"; 
     Stream<String> lines = Files.lines(file, StandardCharsets.UTF_8); 
     for(String line : (Iterable<String>) lines.skip(currentLine)::iterator){ 
      final Pattern pattern = Pattern.compile(regex); 
      final Matcher matcher = pattern.matcher(line.substring(0)); 
      while (matcher.find()) { 
       testPop.add(Double.parseDouble(matcher.group(0))); 
      }    
      currentLine++; 
      if(currentLine%52==0) { 
       for(int i =0;i<52;i++) { 
        int date=4+29*i; 
        int a=13+29*i; 
        int b=6+29*i; 
        int c=15+29*i; 
        int d=16+29*i; 
        int e=8+29*i; 
        int f=17+29*i; 
        int g=14+29*i; 
        int h=7+29*i; 
        WeeklyCalculations.put(Integer.parseInt(String.valueOf((int)((testPop.get(date))/1))),new Calculations(testPop.get(a),3,1,testPop.get(b),testPop.get(c),testPop.get(d),testPop.get(e),testPop.get(f),testPop.get(g),testPop.get(h),testPop.get(date),WeeklyCalculations)); 
       } 
       findZeroStockOuts(); 
       ExcelCreator x = new ExcelCreator(WeeklyCalculations,String.valueOf(((int)(testPop.get(1)/1))),String.valueOf(((int)(testPop.get(2)/1))), noStockouts, stockOuts); 
       x.createExcel(); 
       testPop.clear(); 
       WeeklyCalculations.clear(); 
       counter++; 
       System.out.println(counter + "/" + "67101 - "+TimeUnit.SECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS)); 

      } 
     } 

    } catch (IOException ioe){ 
     ioe.printStackTrace(); 
    } 
    catch(NullPointerException x) { 
     readToExcel(currentLine+52); 
    } 

私は、ループ内でそれらをスキップすることができたが、それは膨大な量により速度が低下し、その約3.5万行を考慮し、それが各反復の後にそれらのすべてをスキップすることがあります。これを行う効率的な方法はありますか?

+0

あなたのコードは、それはあなたがしているかを理解するのは難しいということなので、複雑、と疑問不明です探している。あなたがcsvファイルからサンプル行を投稿したとしても、たとえそれがすべてではないとしても、コードを辿った方が良いでしょう。 'a、b、c ... 'と同じように、これらのフィールドを含む' List'のインスタンスに変換され、 'a'であるインデックス0のキーで' Map'を作成します。 –

答えて

0

遅い理由は、開始からファイルを繰り返し読み込むためです。ブロックの下でコードを入力します。ここでは

final String regex = "\\d*\\.?\\d+"; 
final Pattern pattern = Pattern.compile(regex); 

try (Stream<String> lines = Files.lines(file, StandardCharsets.UTF_8)) { 
    final Iterator<String> iter = lines.iterator(); 

    for (int currentLine = 1; iter.hasNext(); currentLine++) { 
     String line = iter.next(); 
     final Matcher matcher = pattern.matcher(line); // No reason do: line.substring(0) 
     while (matcher.find()) { 
      // testPop.add(Double.parseDouble(matcher.group(0))); 
     } 

     try { 
      if (currentLine % 52 == 0) { 
       for (int i = 0; i < 52; i++) { 
        // TODO 
       } 
      } 

      // TODO: 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
      while (currentLine % 52 != 0 && iter.hasNext()) { 
       iter.next(); 
       currentLine++; 
      } 
     } catch (NullPointerException x) { 
      // readToExcel(currentLine + 52); 
      while (currentLine % 52 != 0 && iter.hasNext()) { 
       iter.next(); 
       currentLine++; 
      } 
     } 
    } 
} 

Fork of StreamExによりコードを簡素化する方法である:

final String regex = "\\d*\\.?\\d+"; 
final Pattern pattern = Pattern.compile(regex); 

try (StreamEx<String> stream = StreamEx.ofLines(file, StandardCharsets.UTF_8)) { 
    stream.splitToList(52).filter(l -> l.size() == 52).forEach(lines -> { 
     lines.stream().forEach(line -> { 
      final Matcher matcher = pattern.matcher(line); // No reason do: line.substring(0) 
      while (matcher.find()) { 
       // testPop.add(Double.parseDouble(matcher.group(0))); 
      } 
     }); 

     try { 
      // TODO: 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } catch (NullPointerException x) { 
      // readToExcel(currentLine + 52); 
     } 
    }); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
関連する問題