I have a list of Strings in exact strict order, which I need to convert to a Map, where key is always going to be an even index, starting from 0 and value is going to be the next element, so list is going to look something like this: [key,value,key,value]. By condition my list has fixed even size, but just to handle case where there is no pair either key or value may be null. It is easy to achieve using standard for loop and put elements based on if condition.
But I am looking for more sophisticated (and short) way to achieve that. Maybe there is some option to group elements by two, and than put them into map as key and value correspondingly. Or maybe some existing solution.
I am grateful for any help.
Here is a piece of code I work with:
Resource resourceFile = applicationContext.getResource(ALPHA2_MAPPING_CSV);
var csvRows = CSVReaderUtil.parseFromCSV(resourceFile);
var alpha3AndAlpha2List = csvRows
.stream()
.flatMap(row -> row.getCells().stream())
.filter(cell -> cell.getHeader().equals("Alpha-2 code") || cell.getHeader().equals("Alpha-3 code"))
.toList();