配列内の値の合計を&グループ化しようとしています。以下のデータでは、追加料金列の値は、別のリストに日付 - ノートの組合せ&が格納されるように合計する必要があります。Java - グループ化とラムダを使用した集計
Link1Link2 & Link3私は何をしているのかわかりません。私が直面している問題がはっきりしていることを願って、指導を待ってください。
データ(Excelから読み込ん):
- 日|ノートNo. |サーチャージ
- 29-Jan-2016 | 1234 | 12.2
- 29-Jan-2016 | 1234 | 10.5
- 28-Jan-2016 | 2468 | 9.25
- 28-Jan-2016 | 2468 | 14.4
- 28-Jan-2016 | 2468 | 13.3
- 27-Jan-2016 | 1357 | 17.2
- 27-Jan-2016 | 1357 | 18.7
コードデータモデル:
public class ModelSurcharge {
private LocalDate cNoteDate;
private int cNote;
private double surcharge;
public ModelBrokerage(LocalDate cNoteDate, int cNote, double surcharge) {
this.cNoteDate = cNoteDate;
this.cNote = cNote;
this.surcharge= surcharge;
}
public LocalDate getcNoteDate() {
return cNoteDate;
}
public int getcNote() {
return cNote;
}
public double getSurcharge() {
return surcharge;
}
}
コードメインクラス:
File fileCNote = null;
try {
fileCNote = new File("c:/surcharge.xlsx");
} catch (IOException e) {
e.printStackTrace();
}
// Read data from excel file
List<List<XSSFCell>> dataSurcharge = iFaceXLSX.readData(fileCNote);
// Skip header line & filter out rows which do not have surcharge amount
dataSurcharge = dataSurcharge .stream().skip(1).filter(p -> p.get(6).getNumericCellValue() > 0).collect(Collectors.toList());
// Transfer data read from excel to the data model
List<ModelSurcharge> surcharges= new ArrayList<>();
dataSurcharge.forEach(e -> {
List<ModelSurcharge> temp = Arrays.asList(new ModelSurcharge(
e.get(0).getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
(int) e.get(1).getNumericCellValue(),
(e.get(3).getNumericCellValue() + e.get(4).getNumericCellValue()) * e.get(6).getNumericCellValue()));
surcharges.addAll(temp);
});
//Output to console
surcharges.stream().collect(groupingBy(Function.identity(),
()->new TreeMap<>(
Comparator.<ModelSurcharge,LocalDate>comparing(p->p.getcNoteDate()).thenComparing(p->p.getcNote())),
Collectors.summingDouble(foo->foo.getSurcharge())))
.forEach((group,surcharge) ->
System.out.println(group.getcNoteDate()+"\t"+group.getcNote()+"\t"+surcharge));
// Collecting to another list does not work
List<ModelSurcharge> output = surcharges.stream()
.collect(groupingBy(p -> p.getcNoteDate(), Collectors.summingDouble(p -> p.getSurcharge())));
エラー:それは持っているので
がCannot resolve method 'getcNoteDate()'
Cannot resolve method 'getSurcharge()'
すべてのリンクと無関係なコードを削除し、関連するコードのみを提供して質問を簡略化して明確にしてください:ModelSurchargeクラス、コンパイルされないコードを示す完全ではあるが最小限の例、完全なコンパイラエラーメッセージ? –
@JBNizet私はあなたからの情報として変更を加えました。私は必要な情報を提供し、私が直面している問題がより明確であることを願っています。ありがとうございました。& – iCoder