Java Map to String mapToString(Map parameterMap)

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

Description

map To String

License

Apache License

Declaration

private static String mapToString(Map<String, String[]> parameterMap) 

Method Source Code

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

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

public class Main {
    private static String mapToString(Map<String, String[]> parameterMap) {
        StringBuilder sb = new StringBuilder().append('[');

        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            sb.append(entry.getKey()).append(" -> ");
            String[] value = entry.getValue();
            if (value != null) {
                sb.append("(");
                appendTo(sb, Arrays.asList(value), ",");
                sb.append(")");
            }/*  w  w w .  j a v  a  2s.co m*/
            sb.append(','); // I know being lazy
        }

        return sb.append(']').toString();
    }

    private static StringBuilder appendTo(StringBuilder appendable,
            Iterable<?> parts, CharSequence separator) {
        Iterator<?> iterator = parts.iterator();
        if (iterator.hasNext()) {
            appendable.append(toString(iterator.next()));
            while (iterator.hasNext()) {
                appendable.append(separator);
                appendable.append(toString(iterator.next()));
            }
        }
        return appendable;
    }

    private static CharSequence toString(Object part) {
        return (part instanceof CharSequence) ? (CharSequence) part : part
                .toString();
    }
}

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, String splitter)
  7. mapToString(String aName, Map aMap, String aPrefix, String aSuffix, String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix, String anArraySeparator, String anEscapeChars, char anEscapeSymbol)
  8. mapToString(String itemName, Map map)
  9. mapToStringArray(Map map)