i'm trying to find a way to take 2 txt files, read them into my java class (put them in hash map) and then connect them into one hash map (which i will write latter into a txt file) something like this: i want txt3 to have all the words from txt1 and txt2
txt1
word1 -> [a] [b] [c]
word2 -> [r]
txt2
word1 -> [z] [d]
word3 -> [k]
txt3 - what i want to create
word1 -> [a] [b] [c] [z] [d]
word2 -> [r]
word3 -> [k]
in my code
wordline[0] = word1
wordline[1] = ->
wordline[2] = [a][b][c]
code
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) {
}
}
so at the end of code basically i have the txt1 and txt2 from above and now im trying to create txt3 somehow, but i'm not sure how to connect the list in case i have duplicate key (and then i just want to add the strings together one after each other and not overwrite)
i'm not even sure if my idea for hash map is the right solution here maybe a different structure?
i saw compute function while reading on the forum but i'm not sure if that can help me and not sure how to use exactly.
hope i post in forum like asked in tips. thanks for the help!