繁体   English   中英

如何制作一个ArrayList来解决LinkedHashMap的“问题”?

[英]how make an ArrayList to solve the “issue” with LinkedHashMap?

我设法制作了一个程序,可以从excel文件中读取数据并将其存储在mysql的表中。 我的程序读取每个文件的第一行,为表创建字段,并将其余数据作为值存储在每一列中。 只是因为那是通过编程方式发生的,所以我选择使用LinkedHashMap读取所有值。 一切正常。 但是,当我完成并测试程序后,我的控制台出现错误。 该错误是由LinkedHashMap引起的,因为如果该映射先前包含该键的映射,则旧值将被指定的值替换。

解析数据的代码如下:

private static List<TableRow> parseExcelColumnData(List sheetData) {
        // read each row from 1 to the last
        ArrayList<TableRow> tousRows = new ArrayList<TableRow>();
        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;
            Cell cell = (Cell) list.get(0); // get the first column
            long currentID = (long) cell.getNumericCellValue();
            for (int j = 0; j < list.size(); j++) { //get the rest
                cell = (Cell) list.get(j); 
                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        ...
                                ...............
                    }
                }
            }
            tousRows.add(new TableRow(currentID, tableFields));
        }

        return tousRows;

    }

任何人都可以帮助我如何使“ tableFields”成为ArrayList,以便不替换旧值?

您不能将同一密钥两次放入哈希映射。 想象一下这样一个名为“ fields”的地图:“ A => 1”。 您可以使用fields.get("A")访问键A,并期望其值为1。如果您现在再次将键A放入地图中,则就像“ A => 1,A => 2” 。 您期望fields.get("A")的结果是什么? 在这种情况下,您无法决定要返回1还是2。

此处要做的事情不是使用将字符串映射为整数的映射,而是将字符串映射为整数数组。 代码如下所示:

    private static List<TableRow> parseExcelColumnData(List<?> sheetData) {
        // read each row from 1 to the last
        ArrayList<TableRow> tousRows = new ArrayList<TableRow>();
        for (int rowCounter = 1; rowCounter < sheetData.size(); rowCounter++) {

            List<?> list = (List<?>) sheetData.get(rowCounter);

            LinkedHashMap<String, List<Integer>> tableFields = new LinkedHashMap<String, List<Integer>>(list.size());
            String str;
            String[] tousFields = new String[list.size()];

            int i = 0;
            Cell cell = (Cell) list.get(0);
            long currentID = (long) cell.getNumericCellValue();
            for (int j = 0; j < list.size(); j++) {
                cell = (Cell) list.get(j); 
                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        put(tableFields, String.valueOf(cell.getNumericCellValue()), cell);
                    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        put(tableFields, cell.getStringCellValue(), cell);
                    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        put(tableFields, String.valueOf(cell.getBooleanCellValue()), cell);
                    } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                        put(tableFields, cell.getStringCellValue(), cell);
                    }
                }
            }
            tousRows.add(new TableRow(currentID, tableFields));
        }

        return tousRows;

    }

    private static void put(LinkedHashMap<String, List<Integer>> tableFields, String cellValue, Cell cell) {
        if (!tableFields.containsKey(cellValue)) {
            tableFields.put(cellValue, new ArrayList<Integer>());
        }

        tableFields.get(cellValue).add(cell.getCellType());
    }

然后,您将获得一个类似于“ A => [1,2]”的地图。

TRy与物体

class MyModel {
   String first;
   Integer second;
}

然后创建一个像这样的ArrayList:

List<MyModel> tableFields = new ArrayList<MyObject>(list.size());

您可以改用IdentityHashMap

也许您还需要将对put(key,value)的调用更改为put(new String(key), value)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM