{}
run-icon
Main.java
import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<String, String> map = parseQuery("code=0&message=OK&transId=XXXX-XXXX-XXXX&redirect=https%3A%2F%2Fpay1.comgate.cz%2Finit%3Fid%3DXXXX-XXXX-XXXX"); System.out.println("Code:\n " + map.get("code")); System.out.println("Message:\n " + map.get("message")); System.out.println("TransId:\n " + map.get("transId")); System.out.println("Redirect:\n " + map.get("redirect")); } /** * Parse query string to Map (Key->Value) */ private static Map<String, String> parseQuery(String input) { return Arrays.stream(input.split("&")) .map(pair -> pair.split("=", 2)) // Split into key-value pairs .filter(keyValue -> keyValue.length == 2) // Ensure both key and value exist .collect(Collectors.toMap( keyValue -> URLDecoder.decode(keyValue[0].trim(), StandardCharsets.UTF_8), keyValue -> URLDecoder.decode(keyValue[1].trim(), StandardCharsets.UTF_8) )); } }
Output