Sandeep sandy Sandeep sandy - 3 months ago 8
Java Question

How to convert map to list?

Map map= {"name": "sandeep", "age": 26};
List list = [ "name=sandeep", "age=26" ];


I have a map like above and would like to change it as below list.
Any help appreciable.

Answer

Try this:

Map<String, String> map = new HashMap<>();        
map.put("name", "sandeep");
map.put("age", "26");

List<String> list = new ArrayList<>();

for (Map.Entry<String, String> entry : map.entrySet()) {
    String field = entry.getKey();
    String value = entry.getValue();            
    list.add("\"" + field + "=" + value + "\"");
}