How to create a loop through LinkedHashMap<String,ArrayList<String>>?

Prostak picture Prostak · May 5, 2011 · Viewed 34.6k times · Source

Please help me to create a loop through LinkedHashMap<String,ArrayList<String>> h:

if (h.get("key1").size() == 0)
    System.out.println("There is no errors in key1.");
else
    System.out.println("ERROR: there are unexpected errors in key1.");

if (h.get("key2").size() == 0)
    System.out.println("There is no errors in key2.");
else
    System.out.println("ERROR: there are unexpected errors in key2.");

if (h.get("key3").size() == 0)
    System.out.println("There is no errors in key3.");
else
    System.out.println("ERROR: there are unexpected errors in key3.");

if (h.get("key4").size() == 0)
    System.out.println("There is no errors in key4.\n");
else
    System.out.println("ERROR: there are unexpected errors in key4.\n");

Answer

Bala R picture Bala R · May 5, 2011

Like this?

for (String key : h.keySet())
{
    System.out.println("Key: " + key);   
    for(String str : h.get(key))
    {
       System.out.println("\t" +str);
    }
}

EDIT:

for (String key : h.keySet())
{
    if(h.get(key).size() == 0)
    {
         System.out.println("There is no errors in " + key) ;
    }
    else
    {
        System.out.println("ERROR: there are unexpected errors in " + key);
    }
}