I have a .csv file full of data on over 500 companies. Each row in the file refers to a particular companies dataset. I need to parse this file and extrapolate data from each to call 4 different web services.
The first line of the .csv file contains the column names. I am trying to write a method that takes a string param and this relates to the column title found in the .csv file.
Based on this param, I want the method to parse the file using Java 8's Stream functionality and return a list of the data taken from the column title for each row/company.
I feel like I am making it more complicated than it needs to be but cannot think of a more efficient way to achieve my goal.
Any thoughts or ideas would be greatly appreciated.
Searching through stackoverflow I found the following post which is similar but not quite the same. Parsing a CSV file for a unique row using the new Java 8 Streams API
public static List<String> getData(String titleToSearchFor) throws IOException{
Path path = Paths.get("arbitoryPath");
int titleIndex;
String retrievedData = null;
List<String> listOfData = null;
if(Files.exists(path)){
try(Stream<String> lines = Files.lines(path)){
List<String> columns = lines
.findFirst()
.map((line) -> Arrays.asList(line.split(",")))
.get();
titleIndex = columns.indexOf(titleToSearchFor);
List<List<String>> values = lines
.skip(1)
.map(line -> Arrays.asList(line.split(",")))
.filter(list -> list.get(titleIndex) != null)
.collect(Collectors.toList());
String[] line = (String[]) values.stream().flatMap(l -> l.stream()).collect(Collectors.collectingAndThen(
Collectors.toList(),
list -> list.toArray()));
String value = line[titleIndex];
if(value != null && value.trim().length() > 0){
retrievedData = value;
}
listOfData.add(retrievedData);
}
}
return listOfTitles;
}
Thanks