Java - Write code to convert string Map To String

Requirements

Write code to convert string Map To String

Demo

//package com.book2s;
import java.util.*;

public class Main {
    public static String stringMapToString(final Map<String, String> map,
            final String seperator) {
        if (map == null) {
            return "";
        }/*from   w  ww  . j  ava  2s  .com*/

        final List<String> tempList = new ArrayList<String>();
        for (final String key : map.keySet()) {
            tempList.add(key + "=" + map.get(key));
        }

        return stringCollectionToString(tempList, seperator);
    }

    public static String stringCollectionToString(
            final Collection<String> c, String seperator) {
        if (c == null || c.isEmpty()) {
            return "";
        }

        if (seperator == null) {
            seperator = ", ";
        }

        final StringBuilder sb = new StringBuilder();
        for (final String value : c) {
            sb.append(value);
            sb.append(seperator);
        }
        sb.delete(sb.length() - seperator.length(), sb.length());
        return sb.toString();
    }
}

Related Exercise