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]

在我的代码中

字线[0]=字1

字线[1]=->

字线[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,但我不确定如何连接列表,以防我有重复的密钥(然后我只想把字符串一个接一个地添加在一起,而不是覆盖(

我甚至不确定我的哈希图想法是否是正确的解决方案,也许是不同的结构?

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

希望我在论坛上发帖,就像在提示中所问的那样。谢谢你的帮助!

使用Java 8特性,dictionary1中的两个Map对象可以组合如下。

注意:已将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]

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

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"));

相关内容

最新更新