Normally I would store URL parameters (GET request parameters) with a HashMap<String,String>
but that doesnt account for URLs like test.php?request=id1&request=id2
. Is there any data structure implemented in Java that can deal with this (and that I can query a parameter by name)? Alternatively, is there a single class somewhere that I can use (no libraries please)?
Asked
Active
Viewed 1,436 times
-1

chacham15
- 13,719
- 26
- 104
- 207
-
Try this out: http://stackoverflow.com/a/11733697/752527 – Hanlet Escaño Jul 14 '14 at 21:45
-
@HanletEscaño that suffers from the exact problem that I am describing – chacham15 Jul 14 '14 at 21:47
2 Answers
1
You can fake a multimap with something like this:
Map<String, List<String>> multimap = new HashMap<String, List<String>>();
See the 'MultiMap' section on this page for more information: The Map Interface

martinez314
- 12,162
- 5
- 36
- 63
0
If you are talking about using java to write servlet or something, as far as I know, the HttpServletRequest class comes with the parameter as a list, and for each parameter, the value is an array.
HttpServletRequest req=...
Enumeration<String> names=req.getParameterNames(); //
String para=names.getNextElement();//return "request"
String[] values=para.getParameterValues();// Should get["id1","id2"]
Now you certainly can use a list for it, like whiskeyspider suggested.
You can also make a wrapper on it before put into a hashMap,even just to serialize it. then we you need to use it, just unwrap it. But I would suggest use a list all the way, you can always just check the length to know if it is a single parameter or list.

Evilsanta
- 1,032
- 11
- 18
-
ok, so you are saying basically the same thing as the other answer. thanks for the confirmation. – chacham15 Jul 14 '14 at 21:57