import java.util.HashMap;
import java.util.Map;
public class ParameterComparator {
/**
* Compares values of URL parameters.
*
* @param paramsMap A map containing URL parameters and their values.
* @param comparisonParamsMap A map containing URL parameters to compare against.
* @return A map containing the differences found, where keys are parameters
* and values are a string describing the difference (or null if no difference).
*/
public static Map<String, String> compareParameters(Map<String, String> paramsMap, Map<String, String> comparisonParamsMap) {
Map<String, String> differences = new HashMap<>();
if (paramsMap == null || comparisonParamsMap == null) {
return differences; // Return empty map for null inputs
}
for (Map.Entry<String, String> comparisonEntry : comparisonParamsMap.entrySet()) {
String parameterName = comparisonEntry.getKey();
String comparisonValue = comparisonEntry.getValue();
String actualValue = paramsMap.get(parameterName);
if (actualValue == null) {
differences.put(parameterName, "Parameter '" + parameterName + "' is missing.");
} else if (!actualValue.equals(comparisonValue)) {
differences.put(parameterName, "Value differs: " + actualValue + " vs " + comparisonValue);
}
}
return differences;
}
public static void main(String[] args) {
// Example Usage (for testing)
Map<String, String> params1 = new HashMap<>();
params1.put("param1", "value1");
params1.put("param2", "value2");
params1.put("param3", "value3");
Map<String, String> params2 = new HashMap<>();
params2.put("param1", "value1");
params2.put("param2", "value4");
params2.put("param4", "value4"); //param4 is only in the comparison map
Map<String,String> diff = compareParameters(params1, params2);
for(Map.Entry<String,String> entry : diff.entrySet()){
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Add your comment