0

The attached screenshot below is the output of the dataset. This is my homework and I have been required to do all the coding according to the functional programming style. I have try to use Java Stream and map to parse the date, but I have no idea to group them by month and weeks, then sum the data.

Data Set Example

Below is my code:

This is the function to read the file and store the data into 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));
    }

This is function to retrieve the column of the dataset and parse the date

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());
    }
mike kong
  • 47
  • 7

1 Answers1

0

If I understand you correctly, one option would be to use .collect(Collectors.groupingBy(...) Java Doc.
For example in your case (guessing on the rest of the code), you could do as follows:

parser
   .stream() // stream of csv rows
   .collect(Collectors.groupingBy(CsvRow::getMonth);

This returns a map of month (whatever type that is) against a list of rows for that month.
Hope that gives you some guidance.