0
HashMap<String, List<String>> filterableMap = new HashMap<>();

    filterableMap.put("department", Arrays.asList("A","B",null));
    filterableMap.put("group", Arrays.asList("C","D",null));       

From the above map i need to dynamically build a queryString like show below.

"SomeURL"/token/filter?department=A&department=B&group=C&group=D

I just used Hashmap we can use any thing as long as we can hold the values in name value pair.

for (Map.Entry<String, List<String>> entry : filterableMap.entrySet()) {
        String key = entry.getKey();
        List<String> value = entry.getValue();

        for(String aString : value){
            System.out.println("key : " + key + " value : " + aString);
            if("department".equalsIgnoreCase(key)) {
               qStrBulderBuilder.append("department =");
               qStrBulderBuilder.append(value);

            }

        }
    }

I am using like above approach , but i need to make sure i need put "=" and "&" in the right places some times we may not get "department" or "group"

3
  • 1
    Read how to use foreach and keyset in hashmap this this this and read this Commented Jun 27, 2015 at 14:48
  • To build the URI. maybe you can use something like: github.com/Mashape/unirest-java, isn't it? Commented Jun 27, 2015 at 18:14
  • 1
    If you're running in a Java EE container, you probably should use UriBuilder. Commented Jun 27, 2015 at 19:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.