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");
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);
}
}
Try this code:
Map<String, ArrayList<String>> a = new LinkedHashMap<String, ArrayList<String>>();
Iterator<Entry<String,ArrayList<String>>> itr = a.entrySet().iterator();
while (itr.hasNext()) {
Entry<String,ArrayList<String>> entry = itr.next();
String key = entry.getKey();
System.out.println("key: " + key);
List<String> list = entry.getValue();
System.out.println("value: " + list);
}
Another way in Java8 is with the foreach()
method
Map<String, List<String>> test1 = new LinkedHashMap<String, List<String>>();
test1.forEach((key,value) -> {
System.out.println(key + " -> " + value);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With