I have a Map<String, Map<String, Double>> variable.
By way of example:
{user0, {Item1, 2.5, Item2, 3.5}}
{user1, {Item1, 3.0, Item2, 2.0}}
I want to take the following output:
{Item1, {user0, 2.5, user1, 3.0}}
{Item2, {user0, 3.5, user2, 2.0}}
I wrote the following code to do that:
for(Map.Entry<String, Map<String, Double>> entry : usersMap.entrySet()){
for(Map.Entry<String, Double> ent : entry.getValue().entrySet()){
people.put(entry.getKey(), ent.getValue());
transform.put(ent.getKey(), people);
}
}
but I take the following:
{Item1, {user0, 3.0, user1, 2.0}}
{Item2, {user0, 3.0, user2, 2.0}}
Can you give me some help here?
You have to create a new map for every item
Otherwise, all values in
transformcontain the exact same map.Also, you might want to rethink your variable names, e.g.:
Seriously, it took me several attempts to get the code right because of the confusing names. But it should be correct now