Java 使用ID对LinkedHashMap进行排序

Java 使用ID对LinkedHashMap进行排序,java,linkedhashmap,Java,Linkedhashmap,我正在使用LinkedHashMap从excel文件中读取数据,并将它们存储在mysql的表中! 如何对LinkedHashMap进行排序以存储ID递减的数据? 下面是我的excel文件的一个示例: ID名称工资 50克里斯汀2349000 43波琳娜1245874 54劳拉4587894 下面的代码用于将excel文件的数据存储在表中 private static LinkedHashMap[] parseExcelColumnData(List sheetData) {

我正在使用LinkedHashMap从excel文件中读取数据,并将它们存储在mysql的表中! 如何对LinkedHashMap进行排序以存储ID递减的数据? 下面是我的excel文件的一个示例:

ID名称工资
50克里斯汀2349000
43波琳娜1245874
54劳拉4587894

下面的代码用于将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;
            }

            return tousRows;

        }
私有静态LinkedHashMap[]parseExcelColumnData(列表数据){
LinkedHashMap[]tousRows=新LinkedHashMap[sheetData.size()-1];
对于(int rowCounter=1;rowCounter
根据,LinkedHashMap“正常”按照密钥添加到映射中的顺序存储密钥,因此您无法真正更改顺序

相反,您可以提取所有键并对其进行排序,例如:

LinkedHashMap<Integer, String> map = new LinkedHashMap <Integer, String>();
/* add elements here */
Set<Integer> keys = new TreeSet<Integer>();
keys.addAll(map.keySet());
LinkedHashMap=newLinkedHashMap();
/*在此处添加元素*/
设置关键帧=新树集();
addAll(map.keySet());

设置键
现在包含所有按排序顺序排列的映射键,因为TreeSet存储元素基于其自然顺序

可能重复@AndrewThompson他没有尝试在搜索框中键入“sort linkedhashmap”我读过关于treeMap和separator的内容,但我没有成功!这就是我在这里问的原因。“我已经阅读了关于树形图和分隔符的文章,但我没有成功”向我们展示您的最佳尝试…,一些链接!我搜索过了,但因为我有问题,我问。