Maybe it's quite a stupid question but I cant find any solution for it. I am new to lambda and I am trying to use it to parse a list <List>
into an ArrayList <HashMap>
.
Initially I parse a data set from csv file, and I turn each row of them into a List, and add them into the List <'List'> I stated above. Since I have to filter them after this, I would like to change the each row of them into a HashMap<columnName, columnValue>
, but when I try to do it, it returns nothing.
Here is my code:
// import data from csv file
private static List<List> readWholeFile(BufferedReader br) throws IOException {
List<List> rows = new ArrayList<>();
String line = br.readLine();
try {
if (line != null) {
String[] test = line.split(",");
List<String> row = Arrays.asList((String[]) line.split(","));
rows.add(row);
return readWholeFile(br);
}
} catch (NullPointerException ex) {
ex.printStackTrace();
} finally {
return rows;
}
}
//parse List<List> into ArrayList<HashMap>
private static ArrayList<HashMap> putRowIntoMap(List<List> rows, List<String> columns) {
ArrayList<HashMap> itemMap = new ArrayList<>();
List<HashMap> test = new ArrayList<>();
HashMap<String, String> eleMap = new HashMap<>();
rows.stream().map(row -> (row.stream().map(ele -> eleMap.put(keys.get(row.indexOf(ele)), (String) ele))))
.collect(Collectors.toCollection(ArrayList::new));
itemMap.add(eleMap);
System.out.println(eleMap); //output: {}
return itemMap;
}