I have a sample csv file as follows (a dummy csv file structure for reference):
Col0, Col1, Col2, Col3, Col4, Col5
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
str1, str2, str3, str4, str5, 45.545
I am trying to read this csv file into a HashMap<String, Double>
where the key is generated as (Col1 + "|" + Col2 + "|" + Col3 + "|" + Col4 + "|" + Col0) and value as Col5
.
I am following a similar article here (How to convert csv to a map using Java 8 Stream ) but getting error while converting Col5 to double as follows:
java.lang.NumberFormatException: empty String
Following is the code I am currently using:
private void convertCsvtoMap(final String filePath) {
try {
Stream<String> lines = Files.lines(Paths.get(filePath));
Map<String, Double> resMap = lines.skip(1).map(line -> line.split(",")).collect(
Collectors.toMap(line -> (line[1] + "|" + line[2] + "|" + line[3] + "|" + line[4] + "|" + line[0]), line -> Double.parseDouble(line[5])));
} catch (IOException e) {
e.getLocalizedMessage();
}
}