fork download
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.AbstractMap.SimpleImmutableEntry;
  4. import static java.util.stream.Collectors.toList;
  5.  
  6. class ParseParams {
  7.  
  8. Map<String, List<String>> parseParams(String url) {
  9.  
  10. return Arrays.stream(url.split("&"))
  11. .map(this::splitQueryParameter)
  12. .collect(Collectors.groupingBy(SimpleImmutableEntry::getKey, LinkedHashMap::new, Collectors.mapping(Map.Entry::getValue, toList())));
  13. }
  14.  
  15. private SimpleImmutableEntry<String, String> splitQueryParameter(String it) {
  16. final int idx = it.indexOf("=");
  17. String key = idx > 0 ? it.substring(0, idx) : it;
  18. String value = idx > 0 && it.length() > idx + 1 ? it.substring(idx + 1) : null;
  19. if (key.contains("<")) {
  20. key = key.replace("<", "");
  21. }
  22. return new SimpleImmutableEntry<>(key, value);
  23. }
  24. }
  25.  
  26. public class Main {
  27.  
  28. public static void main(String[] strg) {
  29.  
  30. String str = "//RULE countryname=Brazil&useryear<=2017&usermonth<=01&userdayofmonth<=15 200";
  31. str = str.substring(str.indexOf(" "), str.lastIndexOf(" "));
  32. try {
  33. ParseParams parse = new ParseParams();
  34. Map<String, List<String>> map = parse.parseParams(str);
  35. map.entrySet().forEach(entry -> {
  36. System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
  37. });
  38. } catch (Throwable t) {
  39. t.printStackTrace();
  40. }
  41. }
  42. }
Success #stdin #stdout 0.09s 711680KB
stdin
Standard input is empty
stdout
Key :  countryname Value : [Brazil]
Key : useryear Value : [2017]
Key : usermonth Value : [01]
Key : userdayofmonth Value : [15]