I asked a question some time back about parsing CSV files for a single matching row. In the example shown below, I use a bufferedreader to read the header row as the first step. Using this row, I parse the column names and then proceed to search for matching rows. The filter criteria I need to search for the matching row should be based on 2 column values, instead the code shown below only returns the 1 row - presumably because I use
.findFirst().get();
Instead I need something along the following lines (but this code is not valid)
List<String> rowCols = reader.lines()
//.skip(1)
.map((line) -> Arrays.asList(line.split(",")))
.filter(list ->
!list.get(col1Index).equalsIgnoreCase("0:00") &&
!list.get(col2Index).equalsIgnoreCase("0:00"))
.findFirst().get();
as this also just returns 1 row - however the filter matches multiple rows.
I now need to return multiple matching rows but I cannot figure out the correct syntax.
String fileName = ...
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(ftpClient.
retrieveFileStream(fileName)))){
List<String> columns = reader.lines()
.findFirst()
.map(line -> Arrays.asList(line.split(",")))
.get();
// find the relevant sections from the CSV file
// we are only interested in the row with the CA ServiceName
int serviceNameIndex = columns.indexOf("ServiceName");
int col1Index = columns.indexOf("Column1");
int col2Index = columns.indexOf("Column2");
// we need to know the index positions of the columns
// also note that due to using a BufferedReader we don't
// have to re-read the csv file to extract the values
List<String> rowCols = reader.lines()
//.skip(1)
.map((line) -> Arrays.asList(line.split(",")))
.filter(list -> list.get(serviceNameIndex).equalsIgnoreCase("service1"))
.findFirst().get();
EnumMap<Parameter, String> params = new EnumMap(Parameter.class) {{
put(Parameter.ServiceName, rowCols.get(serviceNameIndex));
put(Parameter.Column1, rowCols.get(col1Index));
put(Parameter.Column2, rowCols.get(col2Index));
}};
params.put("service1", params);
}