I'm writing a logic for converting data fetched from data base into CSV format. The total number of header fields in CSV file are 14. Now it is possible that not all the field values are available. To give an example the data could be :
__________________________________________
|C1 | C2 | C3 | C4 | C5 |....|C12|C13|C14|
------------------------------------------
| V | V | V | V | V | V | V | | |
| V | V | V | V | V | | | | |
| V | V | V | V | V | | | | |
Now I use an ArrayList to store the data in order of CSV fields. Also the values for first five columns (C1-C5) and the column C12 of CSV file are always available . The values for C6-C11 may or may not be available and they are stored in another list.And this will always be the case.Now to maintain the order, I have to insert empty string in ArrayList. So the code would be like -
ArrayList<String> csvRow = new ArrayList<String>();
// adding mandatory values (C1-C5)
csvRow.add(value); // there will be 5 such statement.
if(!optionalValList.isEmpty()) {
// add the values to csvRow List for the C6-C11 fields
}else {
csvRow.add(""); csvRow.add("");
// do this for C6-C11 fields. So there
// will be 6 such statements to add the empty string to List to
// maintain the order of fields in CSV.
csvRow.add(value); //adding the mandatory C12 field
}
Note : Values C1-C5 and C12 are always available. And this will always be the case.
Now I don't like this approach. So I have come up with a logic using the lambda expression which will replace the logic in else block. It is as below :
public List<String> createList(List<String> inputList){
return IntStream.range(0,14)
.mapToObj(i -> (i < inputList.size())? inputList.get(i):"")
.collect(Collectors.toList());
}
So the else block will be like :
else {
csvRow = createList(csvRow);
csvRow.set(11,value); // setting the column C12
}
Now I want get feedback on this approach. Is this abuse of the lambda features? If yes how can I do this better.