Convert Map<?, ?> To String - Java java.util

Java examples for java.util:Map Entry

Description

Convert Map<?, ?> To String

Demo Code


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

public class Main {
    public static String mapToString(Map<?, ?> map) {
        if (map.isEmpty()) {
            return "";
        }//  w  ww.  j  a  v a2s  . c  o m
        StringBuilder stringBuilder = new StringBuilder();
        boolean notFirst = false;
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            if (notFirst) {
                stringBuilder.append(',');
            }
            stringBuilder.append(entry.getKey().toString()).append(':')
                    .append(entry.getValue().toString());
            notFirst = true;
        }
        return stringBuilder.toString();
    }
}

Related Tutorials