Java Utililty Methods Map to String

List of utility methods to do Map to String

Description

The list of methods to do Map to String are organized into topic(s).

Method

StringmapToString(String aName, Map aMap, String aPrefix, String aSuffix, String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix, String anArraySeparator, String anEscapeChars, char anEscapeSymbol)
map To String
StringBuilder sb = new StringBuilder();
if (aName != null) {
    sb.append(escapeString(aName, anEscapeChars, anEscapeSymbol));
if (aPrefix != null) {
    sb.append(aPrefix);
if (aMap != null && !aMap.isEmpty()) {
...
StringmapToString(String itemName, Map map)
map To String
StringBuffer buffer = new StringBuffer("\n\t< " + itemName + " >\n");
Iterator iterator = map.keySet().iterator();
for (; iterator.hasNext();) {
    Object key = iterator.next();
    Object value = map.get(key);
    buffer.append("\t\t< ").append(null == key ? "null" : key.toString()).append(" = ")
            .append(null == value ? "null" : value.toString()).append(" >\n");
buffer.append("\t< " + itemName + " >");
return buffer.toString();
String[]mapToStringArray(Map map)
map To String Array
final String[] strings = new String[map.size()];
int i = 0;
for (Map.Entry<String, String> e : map.entrySet()) {
    strings[i] = e.getKey() + '=' + e.getValue();
    i++;
return strings;
StringBuildermapToStringBuilder(Map m)
map To String Builder
StringBuilder sb = new StringBuilder();
m.forEach((key, value) -> sb.append(key).append(":").append(value).append("\\"));
sb.append("\n");
return sb;
StringmapToStringList(Map map, String pairDelimiter, String listDelimiter)
Takes a map of objects and converts their keys and values to a list where each key and value are converted to a string by their toString() methods, the pairs are joined with a 'pairDelimiter' between them and the pairs are joined with other pairs with a 'listDelimiter' between them.

The keys and value cannot be null or an exception will be thrown.
boolean firstPass = true;
StringBuilder resultBuilder = new StringBuilder();
for (Object key : map.keySet()) {
    Object value = map.get(key);
    if (value == null) {
        throw new IllegalArgumentException("The map cannot contain null values.");
    if (firstPass) {
...
MapmapToStringMap(final Object map)
map To String Map
if (!(map instanceof Map<?, ?>)) {
    throw new IllegalArgumentException(
            String.format("Cannot cast %s to Map<String, ?>", map.getClass().getName()));
Map<String, String> strMap = new HashMap<String, String>();
for (Map.Entry<?, ?> e : ((Map<?, ?>) map).entrySet()) {
    strMap.put(e.getKey().toString(), e.getValue().toString());
return strMap;
StringtoQueryString(Map ps)
to Query String
StringBuilder buf = new StringBuilder();
if (ps != null && ps.size() > 0) {
    for (Map.Entry<String, String> entry : new TreeMap<String, String>(ps).entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (key != null && key.length() > 0 && value != null && value.length() > 0) {
            if (buf.length() > 0) {
                buf.append("&");
...
StringtoStr(Map msg)
To string.
StringBuilder builder = new StringBuilder("\n---- Begin Map-----\n");
for (Entry<K, V> entry : msg.entrySet()) {
    builder.append(entry.getKey().toString()).append("=").append(entry.getValue().toString()).append("\n");
return builder.append("---- End Map -----\n").toString();
StringtoString(final Map tokens)
to String
StringBuilder buf = new StringBuilder("{ ");
String comma = "";
for (Map.Entry<?, ?> e : tokens.entrySet()) {
    buf.append(comma + e.getKey() + ":" + e.getValue());
    comma = ", ";
return buf.toString();
StringtoString(Map varnames_to_Terms)
Converts a substitution, in the form of a Map from variable names to Terms, to a String.
if (varnames_to_Terms == null) {
    return "[no solution]";
java.util.Iterator varnames = varnames_to_Terms.keySet().iterator();
String s = "Bindings: ";
while (varnames.hasNext()) {
    String varname = (String) varnames.next();
    s += varname + "=" + varnames_to_Terms.get(varname).toString() + "; ";
...