Sandeep sandy Sandeep sandy - 1 year ago 102
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 Source

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 + "\"");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download