How do i get both of the values from each map into another map? One map has the name of the ingredient as key. keywordsToIds has the ID as value and firstCounter has the occurance of the ingredient as value. I want to have a map of ID as key and occurance as value. The keys work but the values don't. I hope someone can help me out. I am very new to maps and arraylists.
Map<String, Long> keywordsToIds
Map<String, Integer> firstCounter
Map<Long, Integer> idAndCount = new HashMap<>();
for (Map.Entry<String, Integer> entry : firstCounter.entrySet())
if (keywordsToIds.containsKey(entry.getKey())){
idAndCount.put(keywordsToIds.get(entry.getKey()), firstCounter.get(entry.getValue()));
}
return idAndCount;
@Test
@DisplayName("can detect multiple occurrences of ingredients")
void testCounting() {
// Input-Daten:
String inputLine = "Ich hätte gerne einen Vollkorn Burger mit Cheddar-Käse Cheddar-Käse und noch mehr Cheddar-Käse";
Map<String, Long> keywordsToIds = Map.of(
"Vollkorn", 19L,
"Cheddar-Käse", 87L,
"Rindfleisch", 77L);
Map<Long, Integer> expected = Map.of(
19L, 1,
87L, 3);
Map<Long, Integer> actual = sut.idsAndCountFromInput(inputLine, keywordsToIds);
assertEquals(expected, actual);
}
expected: <{19=1, 87=3}> but was: <{19=null, 87=null}>
Expected :{19=1, 87=3}
Actual :{19=null, 87=null}
I have tried the loop above, where i say if the key of the one map contains the key of the other map, put the value of keywordsToIds as key and value of firstCounter as value.