Q:

Iterating for loop for list of strings and storing element value

I have a hashmap which has a list of keys which i am interating in a for loop.
In my Hashmap i have one Key FURNITURE_NAMES and values as Table,Chair,Board.
Now i want the values that are present in key for FURNITURE_NAMES be stored in a list.
Below is the code i have done:
When i get the key FURNITURE_NAMES i have the below values
FURNITURE_NAMES = "table,chair,board"
for(Map.Entry<String, List<String>> entry : furnitureMap.entrySet()) 
{
String furnitureKey = entry.getKey();
for(String key : entry.getValue() )
    {   
    if(furnitureKey!=null && furnitureKey.equalsIgnoreCase("FURNITURE_NAMES"))
        {                           
            furnitureList = new ArrayList<String>();
            String a = new String();
            a=key;
            furnitureList.add(a);                                                       
        }
    }
}

When i iterate the Hashmap get the key FURNITURE_NAMES i get the first value "Table"
That value gets stored in the list.
Second i get chair.
It overrides value Table and i still have only 1 value in the list that is chair.
I tried to put the values in the list through index also but it threw indexoutofboundsexception. How do i have all the values in the list instead of getting overridden by new values. Any help.

A:

At each iteration you create a new instance of the list as follows: 
furnitureList = new ArrayList<String>();

You have to create this list before the for or somewhere else and leave it like that as follows:
furnitureList = new ArrayList<String>();
for(Map.Entry<String, List<String>> entry : furnitureMap.entrySet()) {
// same code here except the list initialization
}

Also, you don't need to create a new String for each value. Just put it in the list as the the value variable (which is incorrectly named key :) ) from the for loop. If we refactor your code it would look something like:
furnitureList = new ArrayList<String>();
for(Map.Entry<String, List<String>> entry : furnitureMap.entrySet()) {
    if(entry.getKey() != null && entry.getKey().equalsIgnoreCase("FURNITURE_NAMES")) {
        furnitureList.addAll(entry.getValue());
    }
}

