java - 如何读取文本文件并使用 HashMap 存储?

标签 java hashmap

我有一个文本文件,其中包含 3 列的整数,并且第一列上的每个数字都有多个条目,如上面的示例:

1 23 1
1 24 2
1 57 1
1 10 1
1 59 2
1 31 2
1 38 1
1 11 1
2 39 2
2 40 1
2 15 1
2 74 2

我可以使用上面显示的代码读取文件的内容

public static void main(String[] args) {

    try (BufferedReader br = new BufferedReader(new FileReader("/home/user/Desktop/file.txt")))
        {
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }
        } 
    catch (IOException e) {
                e.printStackTrace();
        } }

但我想使用 hashmap 将第一列的每个不同编号的所有条目连接到一行中,如下所示:

1 23 1 24 2 57 1 10 1 59 2 31 2 38 1
2 39 2 40 1 15 1 74 2

你有什么建议吗? 谢谢!

最佳答案

这里有一些可以帮助您入门的基本代码。这使用了对条目进行排序的 TreeMap。阅读 HashMap 上的简介看看为什么,它指定 HashMap 不保证条目的顺序。可以使用 HashMap 来实现此目的,但我不推荐它,因为您可能需要在某处执行额外的步骤。

TreeMap<Integer, ArrayList<Integer[]>> rowMap = new TreeMap<Integer, ArrayList<Integer[]>>();

// when you have your new line

String[] cols = sCurrentLine.split(" ");

if (cols.length < 1) {
    // handle error if there is not a key column
}

try {
    int colKey = Integer.parseInt(cols[0]);

    Integer[] colValues = new Integer[cols.length - 1];

    for (int i = 1; i < cols.length; i++) {
        colValues[i - 1] = Integer.parseInt(cols[i]);
    }

    if (!rowMap.containsKey(colKey)) {

        // add a new entry for this column number
        rowMap.put(colKey, new ArrayList<Integer[]>());
    }

    rowMap.get(colKey).add(colValues);

} catch (NumberFormatException e) {
    // handle error if any columns don't have integers
}

现在您拥有的是一个 TreeMap,其中包含按第 1 列中的数字分组的所有的列表。

要让示例文档打印整个 map ,您可以执行以下操作:

// iterate by column 1 number in numerical order
for (Integer key : rowMap.keySet()) {

    // iterate by rows in this column in the order they were added
    for (Integer[] row : rowMap.get(key)) {
        String printout = key + ":";

        // iterate by columns in this row
        for (Integer col : row) {
            printout += " " + col;
        }

        System.out.println(printout);
    }
}

这将输出以下内容:

1: 23 1
1: 24 2
1: 57 1
1: 10 1
1: 59 2
1: 31 2
1: 38 1
1: 11 1
2: 39 2
2: 40 1
2: 15 1
2: 74 2

因此,您可以看到这根据第 1 列中的值对文档进行排序。您还可以按照 OP 中显示的格式打印它们,只需稍微更改循环,以便将行添加到同一条线。

有关以这种方式使用 TreeMap 的一些注意事项:

  • 第 1 列中的数字可以是任何整数
  • 第 1 列中的数字可以采用任意顺序。
  • 除了第一列之外,行还可以有任意数量的列。

但是当您使用 map 列出它们时,它们将始终按第 1 列编号进行分组,并且始终按数字排序。

如果您不关心保持行排序,那么它可能会有所简化,在这种情况下您可以创建 TreeMap<Integer, ArrayList<Integer>>TreeMap<Integer, ArrayList<String>>甚至是TreeMap<Integer, String>如果您所做的只是连接每一行。

关于java - 如何读取文本文件并使用 HashMap 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866641/

相关文章:

java - 使用对象或整数作为 HashMap 键更好吗?

java - HashMap 未添加所有键

java - 从数据库表填充 Java HashMap

java.util.HashMap 得到 : does key have to be exactly the same object as what is stored in the HashMap, 或者键可以只是 "equal"

java - 如何将 JavaRDD<Row> 转换为 JavaRDD<List<String>>?

java - com.android.builder.dexing.DexArchiveMergerException : Error while merging dex archives:

java - Item 类中的构造函数 Item 不能应用于给定类型

java - 是否可以将字符串数组放入缓冲的阅读器中?

java - WildFly9.0 - 添加Web服务访问

java awt事件队列/调度线程未知源错误?