Java Map to String toString(Map m)

Here you can find the source of toString(Map m)

Description

to String

License

Apache License

Declaration

public static String toString(Map<Object, String> m) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Map;

public class Main {
    private static final String K_V_SEPARATOR = "=>";

    public static String toString(Map<Object, String> m) {
        if (m == null || m.isEmpty()) {
            return "";
        }/*  w  w w .  ja  v  a  2 s.com*/
        StringBuilder sb = new StringBuilder();
        int n = m.size();
        for (Object key : m.keySet()) {
            String stringKey = String.valueOf(key);

            sb.append('"').append(escapeQuotes(stringKey)).append('"');
            sb.append(K_V_SEPARATOR);
            sb.append('"').append(escapeQuotes(String.valueOf(m.get(key))))
                    .append('"');

            if (n > 1) {
                sb.append(", ");
                n--;
            }
        }
        return sb.toString();
    }

    private static String escapeQuotes(String text) {
        return text.replaceAll("\"", "'");
    }
}

Related

  1. toString(Map m)
  2. toString(Map map)
  3. toString(Map map)
  4. toString(Map map)
  5. toString(Map map)
  6. toString(Map map)
  7. toString(Map map)
  8. toString(Map attrs)
  9. toString(Map attrs)