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.
CodePudding user response:
I'm assuming you're trying to have 2 maps that hold the same strings as the keys, while having an additional map corresponding to an Integer occurrence
and a Long ID
.
To make a Map which holds an Integer occurrence
as the key and a Long ID
as the value, I believe you have two options, either manually going through each value set, or creating a custom class to hold your two properties.
Option One: Creating a custom class
You could create a class representing the thing your trying to store, and create a Map of your Object
. For the sake of simplicity, lets say you are trying to have two maps represent IDs. Each ID Object represents the ID itself, and how frequently its been used (the occurrence).
You could create your class like this:
public class ID {
long id = 0L;
int occurrence = 0;
public ID(long id, int uses) {
this.id = id;
occurrence = uses;
}
public long getID() {
return(id);
}
public int getOccurs() {
return(occurrence);
}
}
Then, make all the ID
objects you need and add them into either a Map, if you still want the string representation, as Map<String, ID>
, or just create a list, because the ID object stores all the properties you were previously storing in the map.
Option Two: Manually Getting Each Value
You could get each of the value Collections
and loop through them at the same time by iterating through each list to add each Long and Integer to the same map.
Map<String, Long> keywordsToIds = new HashMap<String, Long>();
Map<String, Integer> firstCounter = new HashMap<String, Integer>();
String[] list = new String[] {"wordA", "wordB", "wordC"};
long[] longs = new long[] {12L, 14L, 15L};
int[] ints = new int[] {123, 456, 789};
for (int i=0; i<list.length; i ) {
keywordsToIds.put(list[i], longs[i]); // Adds each word pointing to a Long
firstCounter.put(list[i], ints[i]); // Adds each word pointing to an Integer
}
Map<Long, Integer> idsToOccurrence = new HashMap<Long, Integer>();
ArrayList<Long> ids = new ArrayList<Long>(keywordsToIds.values()); // Retrieve the values from the ID map
ArrayList<Integer> occurrence = new ArrayList<Integer>(firstCounter.values()); // Retrieve the values from the Occurrence map
for (int i=0; i<ids.size(); i ) { // Loop through each, value set
idsToOccurrence.put(ids.get(i), occurrence.get(i)); // Add the values to the ID/Occurrence map
}
for (String key : keywordsToIds.keySet()) { // Loops through each item in the map(s)
Long longID = keywordsToIds.get(key);
System.out.println(key " --> " longID " --> " idsToOccurrence.get(longID)); // Displays the keys pointing to each other
}
Hope this helps clarify things for you. If you're still a bit confused about Maps, take a look here.
CodePudding user response:
I got it. I actually just had to use "getKey" on both, instead of get Value for the second map. Actually sat down hours for this simple change. Pretty new to maps so got confused with getKey and getValue
idAndCount.put(keywordsToIds.get(entry.getKey()), firstCounter.get(entry.getKey()));