1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class ParameterComparator {
  4. /**
  5. * Compares values of URL parameters.
  6. *
  7. * @param paramsMap A map containing URL parameters and their values.
  8. * @param comparisonParamsMap A map containing URL parameters to compare against.
  9. * @return A map containing the differences found, where keys are parameters
  10. * and values are a string describing the difference (or null if no difference).
  11. */
  12. public static Map<String, String> compareParameters(Map<String, String> paramsMap, Map<String, String> comparisonParamsMap) {
  13. Map<String, String> differences = new HashMap<>();
  14. if (paramsMap == null || comparisonParamsMap == null) {
  15. return differences; // Return empty map for null inputs
  16. }
  17. for (Map.Entry<String, String> comparisonEntry : comparisonParamsMap.entrySet()) {
  18. String parameterName = comparisonEntry.getKey();
  19. String comparisonValue = comparisonEntry.getValue();
  20. String actualValue = paramsMap.get(parameterName);
  21. if (actualValue == null) {
  22. differences.put(parameterName, "Parameter '" + parameterName + "' is missing.");
  23. } else if (!actualValue.equals(comparisonValue)) {
  24. differences.put(parameterName, "Value differs: " + actualValue + " vs " + comparisonValue);
  25. }
  26. }
  27. return differences;
  28. }
  29. public static void main(String[] args) {
  30. // Example Usage (for testing)
  31. Map<String, String> params1 = new HashMap<>();
  32. params1.put("param1", "value1");
  33. params1.put("param2", "value2");
  34. params1.put("param3", "value3");
  35. Map<String, String> params2 = new HashMap<>();
  36. params2.put("param1", "value1");
  37. params2.put("param2", "value4");
  38. params2.put("param4", "value4"); //param4 is only in the comparison map
  39. Map<String,String> diff = compareParameters(params1, params2);
  40. for(Map.Entry<String,String> entry : diff.entrySet()){
  41. System.out.println(entry.getKey() + ": " + entry.getValue());
  42. }
  43. }
  44. }

Add your comment