Let's say I have the following data structure like this:
LinkedHashMap<String, ArrayList<LinkedHashMap>> foodFamilies = new LinkedHashMap<>();
that looks something like this:
{Fruit = [{Name = Apple,
Color = },
{Name = Cherry,
Color = }],
Vegetables = [{Name = Beet,
Color = }]}
How can I elegantly insert the color "red" for every color of every food in each family?
asked Oct 7, 2015 at 0:46
1 Answer 1
You can do it like this:
for (List<LinkedHashMap> list : foodFamilies.values())
for (LinkedHashMap map : list)
map.put("Color", "red");
By the way, you should not use the raw type LinkedHashMap
. It should be a LinkedHashMap<String, String>
(I think).
I also think that you should be using a class with two fields name
and color
rather than a Map
anyway.
answered Oct 7, 2015 at 0:54
lang-java
Food
class, then use a stream or Groovy closure iterator?