arrays - 多维可查询HashMap数组

标签 arrays csv multidimensional-array java

我有一个 CSV 文件,我想将其存储为 Java 对象。我希望列的名称是数组的第一个维度,键对值是数组的第二个维度。我尝试过不同的解决方案(主要是 LinkedHashMaps),但似乎都无法正常工作。

CSV 看起来像这样:

TimeStamp;Column 1;Column 2;Column3
1385733406;Value1;Value12;Value13
1385733409;Value21;Value22;Value23
1385733411;Value31;Value32;Value33

我希望数组看起来像这样:

["Column 1"]
    ["1385733406","Value1"]
    ["1385733409","Value21"]
    ["1385733411","Value31"]
["Column 2"]
    ["1385733406","Value2"]
    ["1385733409","Value22"]
    ["1385733411","Value32"]
["Column 3"]
    ["1385733406","Value2"]
    ["1385733409","Value22"]
    ["1385733411","Value33"]

这样,我就能够查询对象并检索给定列中的所有键对值,例如,第 1 列中的所有数据。使用 HashMap 似乎不起作用,因为它们需要两个参数,这样做似乎不是正确的方法。这是到目前为止我能想出的代码,我认为这不是正确的轨道,但这是我能想出的全部。我使用的是 OpenJDK 1.7

public class CsvCollection {
    public Map<String,Map<String,Integer>> Results = new LinkedHashMap<String, Map<String,Integer>>();

    public CsvCollection(){

    }

    public void parseCsvResultFile(File csvFile){       
        CSVReader reader = null;
        List myEntries = null;
        try {
            reader = new CSVReader(new FileReader(csvFile.getAbsolutePath()), ';');
        } catch (FileNotFoundException e) {
            System.out.println("Error opening [], aborting parsing");
        }
        try {
            myEntries = reader.readAll();
        } catch (IOException e) {
            System.out.println("Error reading content of CSV file (but file was opened)");
        }

        for(String header: (String[]) myEntries.get(0)){
            Results.put(header, null);
            // What now?
        }       
    }
}

最佳答案

您可以进行如下2处更改来实现您需要的功能。

1)更改以下代码

public Map<String,Map<String,Integer>> Results = new LinkedHashMap<String, Map<String,Integer>>();

public Map<String, List<String[]>> Results = new LinkedHashMap<String, List<String[]>>();

进行此更改是因为对于指定列(如列 1),它有 3 行包含时间戳和相应的列值。您需要使用列表来存储它们。

2)更改以下for循环

   for(String header: (String[]) myEntries.get(0)){
        Results.put(header, null);
        // What now?
    } 

    String[] headerColumns = (String[]) myEntries.get(0);
    // First column is TimeStamp, skip it
    for (int i = 1; i < headerColumns.length; i++) {

        List<String[]> list = new ArrayList<String[]>();
        for (int rowIndex = 1; rowIndex < myEntries.size(); rowIndex++) {
            String[] row = (String[]) myEntries.get(rowIndex);
            list.add(new String[] { row[0], row[i] });
        }

        Results.put(headerColumns[i], list);
    }

完成上述 2 个更改后,如果使用以下代码在控制台中打印结果(Map< String, List< String[] >> 类型),

   for(Map.Entry<String, List<String[]>> entry : Results.entrySet())
    {
        System.out.printf("[%s]\n",entry.getKey());
        for(String[] array : entry.getValue())
        {
            System.out.println(Arrays.toString(array));
        }
    }

您将得到您需要的结果:

[Column 1]
[1385733406, Value1]
[1385733409, Value21]
[1385733411, Value31]
[Column 2]
[1385733406, Value12]
[1385733409, Value22]
[1385733411, Value32]
[Column3]
[1385733406, Value13]
[1385733409, Value23]
[1385733411, Value33]

注意:上面的示例是使用以下 CSV 文件中的内容执行的:

TimeStamp;Column 1;Column 2;Column3
1385733406;Value1;Value12;Value13
1385733409;Value21;Value22;Value23
1385733411;Value31;Value32;Value33

关于arrays - 多维可查询HashMap数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20288310/

相关文章:

python - Ruby 矩阵添加到多维数组

c - 在 "negative"位置填写数组值

java - 从变量创建数组

php - 带左连接的 SQL 查询结果和带 PHP 的结果数组

java - 清除双数组(我认为我做错了)

ios - 将数组添加到另一个数组的第二列

c - 关于数组下标运算符

python - 我在范围内时收到 IndexError

python - 如何使用Python将德语变音符号导出到Excel文件中

csv - Google Apps 脚本 - 转换为 CSV