I´ve got a short question. I am so despaired because of my problem.
I just want to put different Keys with the same Value in my Map by using a loop.
My main executes the function einfuegen() for multiple times. As in the following code block:
Woerterbuch woerterbuch2 = new Woerterbuch2();
for (Medium m : medienliste) {
for (String s : m.getWorte()) {
woerterbuch2.einfuegen(s);
}
}
By the way I have tested all the loops and assignments of the variables.
Now einfuegen() should put all the the Strings s in the Map. See the following code block:
public class Woerterbuch2 implements Woerterbuch{
HashMap<String, Integer> liste = new HashMap<>();
public void einfuegen(String word) {
// I have deleted all the previous unimportant code
liste.put(word, 1);
}
}
My map only contains one entry, although the function einfuegen() is running for more than one time and there are more than one different String that is assigned to word.
Normally my map should contain more than 50 different words, because einfuegen() is executed for more than 50 times.
My assumption is that Java overwrites the connection from 1 to word because 1 is always the same instance of Integer.
If I´m right I still don´t know how to fix it.
Thanks for your help. I am really looking forward to it =)
einfugenmethod just adds an entry of1each time forword, whereas what you want to do is increment it (+1) if it already exists in your map, and if not add a 1 to it.