Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert map to url query string?

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"="cat" 

I want to get:

param1=12&param2=cat 

final output

relativeUrl+param1=12&param2=cat 
like image 982
Ula Krukar Avatar asked May 11 '10 10:05

Ula Krukar


People also ask

How do you convert a Map to a string?

Use Object#toString() . String string = map. toString();

How do you convert a Map key and value in a string?

Convert a Map to a String Using Java Streams To perform conversion using streams, we first need to create a stream out of the available Map keys. Second, we're mapping each key to a human-readable String.


1 Answers

The most robust one I saw off-the-shelf is the URLEncodedUtils class from Apache Http Compoments (HttpClient 4.0).

The method URLEncodedUtils.format() is what you need.

It doesn't use map so you can have duplicate parameter names, like,

  a=1&a=2&b=3 

Not that I recommend this kind of use of parameter names.

like image 65
ZZ Coder Avatar answered Oct 09 '22 21:10

ZZ Coder