简体   繁体   English

打印出包含另一个哈希图的哈希图

[英]Printing out a hash map that contains another hash map

I'm working on a program that takes in X amount of files and then uses multi-threading to count occurrences of words in each file it saves the result in a hash map. 我正在研究一个程序,该程序接收X个文件,然后使用多线程计数每个文件中单词的出现次数,从而将结果保存在哈希图中。

Map<String, Map<String, Integer>>

I have a hash map like the above above in the format < file_Name < Word , occurrences > I want to export the entire map to a text file and save it. 我有一个以上格式的哈希图,格式为<file_Name <Word,出现次数>,我想将整个图导出到文本文件并保存。

Is there a simple solution to printing the entire map (order/sorting doesn't matter)? 是否有一个简单的解决方案来打印整个地图(顺序/排序无关紧要)?

If you want to do it yourself you can: 如果您想自己做,则可以:

Map<String, Map<String, Integer>> dictionaries = null;

PrintWriter pw = null;

for(Map.Entry<String, Map<String, Integer>> dictionaryEntry : dictionaries.entrySet()) {

    for(Map.Entry<String, Integer>  wordCounts :  dictionaryEntry.getValue().entrySet()) {

        pw.print(dictionaryEntry.getKey() + ", " + wordCounts.getKey() + ", " + wordCounts.getValue());

    }

}

HashMap is Serializable so you could just use HashMap是可序列化的,因此您可以使用

ObjectOutputStream : http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html ObjectOutputStreamhttp : //docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html

There are multiple ways to do this, of course. 当然,有多种方法可以做到这一点。 My favourite would be to use google-gson . 我最喜欢的是使用google-gson It's a Java library to convert between JSON and Java objects. 这是一个在JSON和Java对象之间转换的Java库。 So, you will be able to retrieve the nested hash map structure with no trouble and no (well ... 1-2 lines) extra coding. 因此,您将能够轻松地检索嵌套的哈希映射结构,并且无需额外的编码(只需1-2行)。

Here's a detailed gson example . 这是一个详细的gson示例

Here map contains filename --> another map 这里的map包含filename --> another map

And m contains word-------> count 并且m包含word-------> count

   Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
    Map<String, Integer> m = new HashMap<String, Integer>();
    m.put("abcd", 34);
    m.put("xyz", 34);
    map.put("a.txt", m);

    m=new HashMap<String, Integer>();
    m.put("abcd", 34);
    m.put("xyz", 34);
    map.put("b.txt", m);

    for(String s : map.keySet()){
        System.out.println("File name: "+s);
        Map<String, Integer> innerMap = map.get(s);
        for(String str:innerMap.keySet()){
            System.out.println(str+" : "+innerMap.get(str));
        }
        System.out.println("--------------------------------------------------------------------------------");
    }

output: 输出:

File name: b.txt
abcd : 34
xyz : 34
--------------------------------------------------------------------------------
File name: a.txt
abcd : 34
xyz : 34

Or Another way is simply you do like: 或者另一种方式就是您喜欢:

System.out.println(map);

Output: 输出:

{b.txt={abcd=34, xyz=34}, a.txt={abcd=34, xyz=34}}

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

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