Java Map to String mapToString(Map map)

Here you can find the source of mapToString(Map map)

Description

map To String

License

Open Source License

Declaration

public static String mapToString(Map<?, ?> map) 

Method Source Code

//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;
    }
}

Related

  1. mapToString(Map map)
  2. mapToString(Map map)
  3. mapToString(Map map)
  4. mapToString(Map map)
  5. mapToString(Map map)
  6. mapToString(Map map)
  7. mapToString(Map map)
  8. mapToString(Map map)
  9. mapToString(Map acs)