下面附加的屏幕截图是数据集的输出。这是我的作业,我被要求根据函数式编程风格进行所有的编码。我尝试过使用Java Stream和map来解析日期,但我不知道如何按月和周对它们进行分组,然后对数据求和。
下面是我的代码:
这是读取文件并将数据存储到arraylist中的函数
public ArrayList<List<String>> csvParser(String CSVFileName) throws IOException {
CSVParser csvParser = CSVParser.parse(Paths.get(CSVFileName), Charset.defaultCharset(), CSVFormat.DEFAULT);
return csvParser
.stream()
.map(i->i.toList())
.collect(Collectors.toCollection(ArrayList::new));
}此函数用于检索数据集的列并解析日期
public List<String> parseRetrievedDateList(List<String> dateToBeParsedList){
return dateToBeParsedList.stream()
.map(l-> {
return LocalDate.parse(l,DateTimeFormatter.ofPattern("M/d/yy")).format(DateTimeFormatter.ofPattern("yyyy/M/d"));
})
.collect(Collectors.toList());
}发布于 2021-10-27 14:41:19
如果我理解正确的话,一种选择是使用.collect(Collectors.groupingBy(...) Java Doc。
例如,在您的示例中(猜测代码的其余部分),您可以执行以下操作:
parser
.stream() // stream of csv rows
.collect(Collectors.groupingBy(CsvRow::getMonth);这将根据该月份的行列表返回一个月(不管是哪种类型)的映射。
希望这能给你一些指导。
https://stackoverflow.com/questions/69740562
复制相似问题