提升ID的树形图

时间:2013-04-25 13:41:27

标签: java treemap linkedhashmap

我有一个程序,我必须从excel文件中读取数据并将它们存储在数据库中。我使用LinkedHashmap将每个单元格作为字符串读取。我的excel文件包含以上数据:

  

ID NAME SALARY

     

5 christine 2349000

     

6 paulina 1000

     

7 laura 12587458

     

8 efi 34567

     

43 jim 45878

当我运行程序时,我已成功获得我想要的结果。我现在的问题是我想通过升序ID将数据存储在我的数据库中。 我将如何做到这一点?我知道我必须使用treeMap但究竟如何呢? 以下代码用于将数据存储在数据库中。我正在读取excel文件第二行的数据。

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;
        }

1 个答案:

答案 0 :(得分:0)

将树形图键(Ids)存储为整数,而不是字符串;否则,它们将按字典顺序而不是按数字顺序进行比较。

Integer intId = new Integer(stringId);

现在你只需要分配TreeMap<Integer, Object>(或TreeMap<Integer, String>或其他什么,具体取决于你所存储的内容),其中键是整数ID,值是要存储的对象

TreeMap<Integer, Object> treeMap = new TreeMap<Integer, Object>();
treeMap.put(intId, obj);

当需要读出值时,使用treeMap.values(),您将获得树形图中值的有序集合。