Reputation: 461
I m trying to read the date as key from a comma separated file and MeterID(Which is the first item in the file) as the value. But my problem is, the file has the same date in multiple lines and when I m trying to read the file into a hashmap, I m getting extra null value with every key.
Data in the file is as follow
MeterID,ServiceLocationID,ESIID,ReadingDate,ReadingTime,kWh,rxKWh,KVARHImprt,KVARhExprt,kWhImprtRateA,kWhImprtRateB,kWhExprtRateA,kWhExprtRateB,kVARhImprtRateA,kVARhImprtRateB,kVARhExprtRateA,kVARhExprtRateB,kVARhTotalRateA,kVARhTotalRateB,ExprtkVARhTotalRateA,ExprtkVARhTotalRateB
1182798,,,13/04/2018,12:00:00AM,12391.000,0.000,,,6196.991,6194.366,0.074,0.000,1119.000,1132.000,0.000,0.000,,,,
1182799,,,14/04/2018,12:00:00AM,690.000,0.000,,,348.468,342.449,0.074,0.000,95.000,96.000,0.000,0.000,,,,
1182800,,,15/04/2018,12:00:00AM,2863.000,0.000,,,1133.865,1729.365,0.074,0.000,37.000,21.000,156.000,135.000,,,,
1182801,,,16/04/2018,12:00:00AM,1578.000,0.000,,,1065.586,512.603,0.074,0.000,225.000,199.000,27.000,14.000,,,, 1182802,,,17/04/2018,12:00:00AM,2059.000,0.000,,,716.310,1343.338,0.074,0.000,58.000,85.000,63.000,78.000,,,,
1182803,,,18/04/2018,12:00:00AM,1824.000,0.000,,,755.363,1069.038,0.074,0.000,234.000,289.000,65.000,73.000,,,, 1182804,,,18/04/2018,12:00:00AM,1824.000,0.000,,,755.363,1069.038,0.074,0.000,234.000,289.000,65.000,73.000,,,,
See below code to read data from the file into a hashmap
String filePath = "electric-enhanced_0000_201804130555.txt";
String delimiter = ",";
HashMap<String, String> map = new HashMap<>();
try (Stream<String> lines = Files.lines(Paths.get(filePath)).skip(1)) {
lines.filter(line -> line.contains(delimiter)).forEach(line -> map.put(line.split(delimiter)[3],
line.split(delimiter)[0] + "," + map.get(line.split(delimiter)[3])));
}
System.out.println(map);
Excepted result is
{15/04/2018=1182800, 14/04/2018=1182799, 17/04/2018=1182802, 18/04/2018=1182804,1182803, 16/04/2018=1182801, 13/04/2018=1182798}
Actual result is - null is coming with every key which i want to exclude
{15/04/2018=1182800,null, 14/04/2018=1182799,null, 17/04/2018=1182802,null, 18/04/2018=1182804,1182803,null, 16/04/2018=1182801,null, 13/04/2018=1182798,null}
Upvotes: 0
Views: 1069
Reputation: 26
I think you just need to add a check for null value. Here is the snippet
map.put(line.split(delimiter)[3],map.get(line.split(delimiter)[3])==null?line.split(delimiter)[0]:
line.split(delimiter)[0] + "," + map.get(line.split(delimiter)[3])));
Upvotes: 0
Reputation: 2945
It should be enough to write:
lines.stream().filter(line -> line.contains(delimiter)).forEach(line -> map.put(line.split(delimiter)[3],
line.split(delimiter)[0]));
You may also want to replace your HashMap
with for example org.apache.commons.collections.map.MultiValueMap
which will let you store multiple values under the same key.
So:
Map<String, String> map = new MultiValueMap();
I added one line to your test data and now date 18/04/2018 appears twice, and output is as {15/04/2018=[1182800], 14/04/2018=[1182799], 18/04/2018=[1182803, 1182804], 16/04/2018=[1182801], 13/04/2018=[1182798]}
Upvotes: 1