build Query Parameters String - Java java.net

Java examples for java.net:URL Query

Description

build Query Parameters String

Demo Code

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

import java.util.Map;
import java.util.Set;

public class Main {
    public static String buildQueryParameters(
            Map<String, String> parameterMap) {

        StringBuilder sbQueryParam = new StringBuilder();

        if (parameterMap != null && !parameterMap.isEmpty()) {
            Set<String> keySet = parameterMap.keySet();
            Iterator<String> keyIterator = keySet.iterator();
            while (keyIterator.hasNext()) {
                String key = keyIterator.next();
                if (!parameterMap.get(key).isEmpty()) {
                    sbQueryParam.append(key);
                    sbQueryParam.append("=");
                    sbQueryParam.append(parameterMap.get(key));
                    sbQueryParam.append("&");
                }/*from  ww w . java  2  s . c  om*/
            }
            sbQueryParam.deleteCharAt(sbQueryParam.length() - 1);
        }

        return sbQueryParam.toString();
    }
}

Related Tutorials