i am making a program to read data from excel files and store them in tables. But since I am new to Comparators in Java i have difficulties in making one of them. Here is my issue, I have managed to read all the data from excel files as a string and store them in a table. But my project is to store them in table by ascending IDs. Can someone help me to create a Comparator to compare LinkedHashmaps and store them by their IDs? The data that I have to store is like the below:
ID Name Salary
50 christine 2349000
43 paulina 1245874
54 laura 4587894
The code for parsing the data is the below:
private static LinkedHashMap parseExcelColumnTitles(List sheetData) {
List list = (List) sheetData.get(0);
LinkedHashMap < String, Integer > tableFields = new LinkedHashMap(list.size());
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
tableFields.put(cell.getStringCellValue(), cell.getCellType());
}
return tableFields;
}
private static LinkedHashMap[] parseExcelColumnData(List sheetData) {
LinkedHashMap[] tousRows = new LinkedHashMap[sheetData.size() - 1];
for (int rowCounter = 1; rowCounter < sheetData.size(); rowCounter++) {
List list = (List) sheetData.get(rowCounter);
LinkedHashMap < String, Integer > tableFields = new LinkedHashMap(list.size());
String str;
String[] tousFields = new String[list.size()];
int i = 0;
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
tableFields.put(String.valueOf(cell
.getNumericCellValue()), cell.getCellType());
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
tableFields.put(cell.getStringCellValue(), cell
.getCellType());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
tableFields.put(String.valueOf(cell
.getBooleanCellValue()), cell.getCellType());
}
}
}
tousRows[rowCounter - 1] = tableFields;
}
return tousRows;
}
See Question&Answers more detail:os