So I am currently making a REST app where I receive excel files through requests ,then I use Tablesaw to read excel files and MongoTemplate to save tables in MongoDb . I created this simple function to map results in a way that it looks clean when I save it in MongoDB.
public static List<Map<String, Object>> createMapFromExcelTable(Table table) {
List<String> colNames= table.columnNames();
List<Map<String,Object>> mapList = new ArrayList<>();
Map<String,Object> recordMap;
Map<String,Object> fieldsMap;
for (int i = 0; i < table.rowCount(); i++) {
recordMap=new HashMap<>();
fieldsMap=new HashMap<>();
String id= IdUtil.getShortId();
recordMap.put("recordId",id);
for (String colName : colNames) {
fieldsMap.put(colName,table.row(i).getObject(colName));
}
recordMap.put("fields",fieldsMap);
mapList.add(recordMap);
}
return mapList;
}
Here is the example of what saved documents look like in MongoDB https://prnt.sc/2388js5
Any tips on how to improve this function in any way? Something just doesnt feel right, I think I can make it more optimized and clean. Especially this part
recordMap=new HashMap<>();
fieldsMap=new HashMap<>();
Dont like the idea of creating new HashMaps but I guess cant escape that .