Here you can find the source of mapToString(Map, ?> map)
public static String mapToString(Map<?, ?> map)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Map; import java.util.Map.Entry; public class Main { public static String mapToString(Map<?, ?> map) { return mapToString("\n", ",", map); }/* w w w . ja va 2 s .co m*/ public static String mapToString(String mapSep, String listSep, Map<?, ?> map) { StringBuilder sb = new StringBuilder(); for (Entry<?, ?> e : map.entrySet()) { sb.append(e.getKey()).append(": "); if (e.getValue() instanceof Collection<?>) { listToStringHelper(sb, listSep, (Collection<?>) e.getValue()); } else { sb.append(e.getValue().toString()); } sb.append(mapSep); } if (sb.length() > 0) { sb.setLength(sb.length() - mapSep.length()); } return sb.toString(); } private static StringBuilder listToStringHelper(StringBuilder sb, String sep, Collection<?> list) { for (Object line : list) { sb.append(line).append(sep); } if (sb.length() > 0) { sb.setLength(sb.length() - sep.length()); } return sb; } }