I have a HashMap in which sentence records are stored with an associated key. Now, an output should be created in this displayed how much the word (in my example the word "car") in this index (key) occurs. For example, if the word "car" occurs in the index (key) 5, 4 times, the 5 should also be output 4 times.
The current output is: Car : [1, 2, 3, 5]
My desired output is: Car : [1, 1, 2, 3, 3, 5, 5, 5, 5]
Map<Integer, String> carHashMap = new HashMap<>();
ArrayList<Integer> showCarsInMap = new ArrayList<>();
carHashMap.put(1, "this car is very fast car");
carHashMap.put(2, "i have a old car");
carHashMap.put(3, "my first car was an mercedes and my second car was an audi");
carHashMap.put(4, "today is a good day");
carHashMap.put(5, "car car car car");
for (Map.Entry<Integer, String> entrySection : carHashMap.entrySet()) {
if (entrySection.getValue().contains("car")) {
showCarsInMap.add(entrySection.getKey());
}
}
System.out.println("Car : " + showCarsInMap);
I guess I have to add an additional if-loop, but I don't know how my program can recognize which "car" has already been counted and which not.