Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

java - comparator for LinkedHashMap

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...