2021-02-25

What is the alternative to entrySet of map and the use of stream in kotlin?

I have constructed a HashMap which contains a mapping of field name to value. The mapping will be used to construct a url query string.

Here is my code:

val map = HashMap<String, Any>();
// Build the map
return map.entrySet().stream()
        .map { p -> urlEncodeUTF8(p.getKey()) + "=" + urlEncodeUTF8(p.getValue()) }
        .reduce { p1, p2 -> p1.toString() + "&" + p2 }
        .orElse("")

However kotlin does not like the function entrySet().

It complaints about Cannot access 'entrySet': it is package-private in 'HashMap'.

forEach is not the right answer for me because I want to use the stream syntax to reduce the results to a single string.

I have tried createEntrySet and entries but neither of them supports stream.



from Recent Questions - Stack Overflow https://ift.tt/3aR01SL
https://ift.tt/eA8V8J

No comments:

Post a Comment