Java URL Build buildQuery(final Map query)

Here you can find the source of buildQuery(final Map query)

Description

build Query

License

Open Source License

Declaration

public static String buildQuery(final Map<String, Object> query)

Method Source Code

    //package com.java2s;
    //License from project: Open Source License 

    import java.io.UnsupportedEncodingException;

    import java.net.URLEncoder;
    import java.util.Map;

    public class Main {
        public static String buildQuery(final Map<String, Object> query)
{
    if ((query == null) || query.isEmpty())
    {/*from w w  w  .jav  a  2  s .com*/
        return "";
    }
    return query.entrySet().stream().map(entry -> createQueryElement(entry.getKey(), (entry.getValue() == null) ? null : entry.getValue().toString())).reduce((s1, s2) -> s1 + "&" + s2).orElse("");
}

        public static String createQueryElement(final String key,
                final String value) {
            String result = encodeUTF8(key);
            if (value != null) {
                result += "=" + encodeUTF8(value);
            }
            return result;
        }

        public static String encodeUTF8(final String str) {
            if (str == null) {
                return "";
            }
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (final UnsupportedEncodingException e) { // should never be thrown
                throw new RuntimeException(e);
            }
        }
    }

Related

  1. buildQuery(final Map query)
  2. buildQuery(Map paramMap)
  3. buildQuery(Map paramMap)
  4. buildQuery(Map query)
  5. buildQuery(Map params)