Here you can find the source of mapToString(Map
public static <K, V> String mapToString(Map<K, V> m)
//package com.java2s; //License from project: Open Source License import java.util.*; import java.util.Map.Entry; public class Main { public static <K, V> String mapToString(Map<K, V> m) { if (m == null) return "null"; Set<Entry<K, V>> set = m.entrySet(); int max = set.size() - 1; if (max == -1) return "{}"; StringBuilder sb = new StringBuilder(); Iterator<Entry<K, V>> it = set.iterator(); sb.append('{'); for (int i = 0;; i++) { Entry<K, V> e = it.next(); K key = e.getKey();// ww w. j a va2s . c om V value = e.getValue(); sb.append(key == m ? "(this Map)" : (key == null ? "null" : key.toString())); sb.append('='); sb.append(value == m ? "(this Map)" : (value == null ? "null" : value.toString())); if (i == max) return sb.append('}').toString(); sb.append(", "); } } public static String toString(Object o) { return String.valueOf(o); } }