I have a list of maps List<Map<String,String>> with a name streams.
And it looks like this:
{
"key1" : "value1",
"key2" : "value2"
},
{
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
},
{
"key1" : "value1",
"key2" : "value2"
}
I do a loop on the map and want to add entry in specific condition this is my code:
public void addValue(String name, String value)
{
for (Map<String,String> map :streams)
{
if (map.get("name").equals(name))
map.put("newValue", value);
}
}
I'm getting an exception:
java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:714)
I tried to write this:
public void addValue(String name, String value)
{
for (Iterator<Map<String,String>> map :streams.iterator())
{
if (map.get("name").equals(name))
map.put("newValue", value);
}
}
But the compiler indicates an error on the row : streams.iterator() with the error:
Can only iterate over an array or an instance of java.lang.Iterable
How can I fix this?