how can i use java streams to convert a convert a csv file of header and lines to an array of hashmap ? eg.
orderNo, totals, charges, taxes, payments
ord121,1500.00,30.00,25.00,Paid
ord8925,1700.00,130.00,75.00,Paid
ord7115,300.00,130.00,75.00,Paid
Array[0] of Hashmap should be with Name as orderNo and value as ord121
Array[1] of Hashmap should be with Name as orderNo and value as ord8925... and so on
tried this but stuck on how to make the row(0) content as hashmap name
public static void readFileToMap() {
Pattern pattern = Pattern.compile(",");
String csvFile = inputDirPreFix + "input/file2.tsv";
try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
Map<String,String> namefreq = in
.lines()
.skip(1)
.map(lineData -> pattern.split(lineData))
.collect(HashMap::new, (map, lineData) ->
map.put(lineData[0], lineData[1]),
Map::putAll);
namefreq.forEach((k, v) -> System.out.println(k + " => " + v));
}
catch (Exception ex) {
Logger.getLogger("FileProcessing").log(Level.SEVERE, null, ex);
}
}