Jin Kwon :
I have a Map<String, List<Object>>
.
How can I make it into a Stream of Entry<String, Object>
so that I can construct a concatenated query String
?
q1 a, b
q2 c, d
into
q1=a&q1=b&q2=c&q2=d
I'm, currently, doing this.
if (params != null && !params.isEmpty()) {
final boolean[] flag = new boolean[1];
params.forEach((n, vs) -> {
vs.forEach(v -> {
builder.append(flag[0] ? '&' : '?')
.append(n)
.append('=')
.append(v);
if (!flag[0]) {
flag[0] = true;
}
});
});
}
Eran :
Well, you don't have to produce a Entry<String, Object>
. You can use flatMap
to obtain the key=value String
s and directly construct the query String
using Collectors.joining
:
String query =
map.entrySet()
.stream()
.flatMap(e -> e.getValue().stream().map(v -> e.getKey() + '=' + v))
.collect(Collectors.joining("&"));
Input :
{q1=[a, b], q2=[c, d]}
Output :
q1=a&q1=b&q2=c&q2=d
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=453070&siteId=1