Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
252 views
Welcome To Ask or Share your Answers For Others

1 Answer

you can use TreeMap instead of LinkedHashMap, because you dont want to retain the order of insertion, and for sorting TreeMap is better choice. Read this too Java TreeMap Comparator


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...