Please, help me convert a HashMap<String, ArrayList<String>> to HashMap <String, String>, where each ArrayList should be converted into one String that contains the ArrayList's elments.
1 Answer
I believe that this is what you are looking for.
//HashMap<String, ArrayList<String>> hashMap;
//initialized somewhere above
HashMap<String, String> newHashMap = new HashMap<>();
for (Map.Entry<String, ArrayList<String>> entry : hashMap.entrySet())
{
newHashMap.put(entry.getKey(), entry.getValue().toString());
}
Not sure if your issue was with toString() or how to iterate over a HashMap, if it was the latter, here's how to iterate over a map.
Here's a from-start-to-finish example:
ArrayList<String> al = new ArrayList<>();
al.add("The");
al.add("End");
HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
hashMap.put("This is", al);
HashMap<String, String> newHashMap = new HashMap<>();
for (Map.Entry<String, ArrayList<String>> entry : hashMap.entrySet())
{
newHashMap.put(entry.getKey(), entry.getValue().toString());
}
System.out.println(newHashMap.toString());
//prints {This is=[The, End]}
ArrayList<String>toString(some example showing input and result would be nice) and also what part of your code doesn't work as you expect (you have some code right?)toStringon the entire Map? That should fulfill your requirements.