Java连接2个包含重复键的哈希图

时间:2018-12-01 00:29:52

标签: java

我正试图找到一种方法来接收2个txt文件,将它们读入我的java类(将它们放入哈希图),然后将它们连接到一个哈希图(我将后者写入txt文件) 像这样的东西: 我希望txt3具有txt1和txt2中的所有单词

txt1

word1 -> [a] [b] [c]
word2 -> [r]

txt2

word1 -> [z] [d] 
word3 -> [k]

txt3-我要创建的内容

word1 -> [a] [b] [c] [z] [d]
word2 -> [r]
word3 -> [k]

在我的代码中

wordline [0] = word1

wordline [1] =->

wordline [2] = [a] [b] [c]

代码

public void readPartialPosting() {
    HashMap<String, String>[] dictionary1 = new HashMap[2];
    dictionary1[0]=new HashMap<>();
    dictionary1[1]=new HashMap<>();
    File fromFile;
    BufferedReader br = null;
    try {
        fromFile = new File("1.txt");
        br = new BufferedReader(new FileReader(fromFile));
        String st;
        String[] wordLine;
        String term;
        String termLocation;
        while ((st = br.readLine()) != null) {
            wordLine = st.split(" ", 3);
            term = wordLine[0];
            termLocation = wordLine[2];
            dictionary1[0].put(term, termLocation);
        }
        fromFile = new File("2.txt");
        br = new BufferedReader(new FileReader(fromFile));
        while ((st = br.readLine()) != null) {
            wordLine = st.split(" ", 3);
            term = wordLine[0];
            termLocation = wordLine[2];
            dictionary1[1].put(term, termLocation);
        }
    } catch (Exception e) {
    }

}

所以在代码末尾,基本上我从上面有txt1和txt2,现在我正试图以某种方式创建txt3,但是如果我有重复的密钥,我不确定如何连接列表(然后我只想将字符串一个接一个地添加在一起并且不会覆盖)

我什至不确定我对哈希映射的想法是否是正确的解决方案,这里的结构可能不同?

我在论坛上阅读时看到了计算功能,但是我不确定这是否对我有帮助,也不确定如何使用。

希望我在论坛中发帖,如提示中所述。 谢谢您的帮助!

2 个答案:

答案 0 :(得分:2)

使用Java 8功能,Map中的两个dictionary1对象可以按以下方式组合。

注意:将dictionary1从数组更改为List,以消除有关通用数组的编译器警告/错误。如果可以避免,则不应使用通用数组。

解决方案(Java 8 +)

Map<String, String> merged = new TreeMap<>();
dictionary1.forEach(d -> 
    d.forEach((k, v) ->
        merged.merge(k, v, (v1, v2) -> v1 + " " + v2)
    )
);

HashMap更改为TreeMap,使结果按字母顺序排列。

测试(Java 9 +)

List<Map<String, String>> dictionary1 = List.of(
        Map.of("word1", "[a] [b] [c]",
               "word2", "[r]"),
        Map.of("word1", "[z] [d]",
               "word3", "[k]")
);
// code from above here
merged.entrySet().forEach(System.out::println);

输出

word1=[a] [b] [c] [z] [d]
word2=[r]
word3=[k]

答案 1 :(得分:0)

首先,我建议您将创建文件的字典封装到单独的方法中

public static Map<String, String> createFileDictionary(String path) throws IOException {
    File fromFile = new File(path);

    try (BufferedReader br = new BufferedReader(new FileReader(fromFile))) {
        Map<String, String> dictionary = new HashMap<>();
        String str;

        while ((str = br.readLine()) != null) {
            String[] parts = str.split(" ", 3);
            dictionary.put(parts[0], parts[2]);
        }

        return dictionary;
    }
}

然后使用您可以准备文件,并将所有词典中的项目放入单个地图中。

// java 8 or higher
private static void putAll(Map<String, String> dest, Map<String, String> src) {
    src.forEach((key, val) -> dest.merge(key, val, (s1, s2) -> s1 + ' ' + s2));
}

// java 7 or earlier
private static void putAll(Map<String, String> dest, Map<String, String> src) {
    for (Map.Entry<String, String> entry : src.entrySet()) {
        if (dest.containsKey(entry.getKey()))
            dest.put(entry.getKey(), dest.get(entry.getKey()) + ' ' + entry.getValue());
        else
            dest.put(entry.getKey(), entry.getValue());
    }
}

并在客户代码中使用它:

Map<String, String> fileDictionary = new HashMap<>();
putAll(fileDictionary, createFileDictionary("1.txt"));
putAll(fileDictionary, createFileDictionary("2.txt"));