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;
            }
        });
    });
}
                Learn various ways of Collecting a Stream of List into Map using Java Streams API. Using Collectors.toMap and Collectors.groupingBy with example. Let’s consider, you have a User class and a List of the users which you want to convert to Map.
Example 1 : Stream map () function with operation of number * 3 on each element of stream. Example 2 : Stream map () function with operation of converting lowercase to uppercase.
Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. In this article, the methods to convert a stream into a map is discussed. Method 1: Using Collectors.toMap () Function
The Collectors.toMap () method takes two parameters as the input: KeyMapper: This function is used for extracting keys of the Map from stream value. ValueMapper: This function used for extracting the values of the map for the given key. The following are the examples of the toMap function to convert the given stream into a map:
Well, you don't have to produce a Entry<String, Object>. You can use flatMap to obtain the key=value Strings 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
                        If you have Guava, you might want to consider using a ListMultimap<String, Object> instead of Map<String, List<Object>>, and create your string like so:
String query = Joiner.on("&").withKeyValueSeparator("=").join(map.entries());
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With