1

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
  • Refactor to use a Food class, then use a stream or Groovy closure iterator? Commented Oct 7, 2015 at 0:52

1 Answer 1

2

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.