Do you know of any utility class/library, that can convert Map into URL-friendly query string?
Example:
I have a map:
\"param1\"=12,
\"param2
I wanted to build on @eclipse's answer using java 8 mapping and reducing.
protected String formatQueryParams(Map params) {
return params.entrySet().stream()
.map(p -> p.getKey() + "=" + p.getValue())
.reduce((p1, p2) -> p1 + "&" + p2)
.map(s -> "?" + s)
.orElse("");
}
The extra map
operation takes the reduced string and puts a ?
in front only if the string exists.