{"record":{"id":"3de0eebfdb20677e","repo":"apache/dubbo","slug":"pairs-must-be-even","errorCode":null,"errorMessage":"pairs must be even.","messagePattern":"pairs must be even\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java","lineNumber":216,"sourceCode":"    private static boolean objectEquals(Object obj1, Object obj2) {\n        if (obj1 == null && obj2 == null) {\n            return true;\n        }\n        if (obj1 == null || obj2 == null) {\n            return false;\n        }\n        return obj1.equals(obj2);\n    }\n\n    public static Map<String, String> toStringMap(String... pairs) {\n        Map<String, String> parameters = new HashMap<>();\n        if (ArrayUtils.isEmpty(pairs)) {\n            return parameters;\n        }\n\n        if (pairs.length > 0) {\n            if (pairs.length % 2 != 0) {\n                throw new IllegalArgumentException(\"pairs must be even.\");\n            }\n            for (int i = 0; i < pairs.length; i = i + 2) {\n                parameters.put(pairs[i], pairs[i + 1]);\n            }\n        }\n        return parameters;\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public static <K, V> Map<K, V> toMap(Object... pairs) {\n        Map<K, V> ret = new HashMap<>();\n        if (pairs == null || pairs.length == 0) {\n            return ret;\n        }\n\n        if (pairs.length % 2 != 0) {\n            throw new IllegalArgumentException(\"Map pairs can not be odd number.\");\n        }","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java#L198-L234","documentation":"Thrown by CollectionUtils.toStringMap when the varargs 'pairs' array has an odd length. The method treats arguments as key,value,key,value,... pairs; an odd count means the last key has no matching value, which would produce a malformed map, so it is rejected.","triggerScenarios":"CollectionUtils.toStringMap(pairs...) where pairs.length % 2 != 0 — e.g. toStringMap(\"k1\",\"v1\",\"k2\") (3 args). Empty/null arrays are allowed (return empty map); only non-empty odd lengths throw.","commonSituations":"Programmatically building a parameter map from a list whose source had an odd number of elements; a trailing key with no value in a config builder; copy-paste dropping one element of a pair.","solutions":["Ensure the arguments come in complete key/value pairs — add the missing value or remove the dangling key.","Build the map from a List or Map source first and check even size before spreading into toStringMap.","Validate pairs.length % 2 == 0 before the call if the source is dynamic."],"exampleFix":"// before\nMap<String,String> m = CollectionUtils.toStringMap(\"k1\",\"v1\",\"k2\"); // throws\n\n// after\nMap<String,String> m = CollectionUtils.toStringMap(\"k1\",\"v1\",\"k2\",\"v2\");","handlingStrategy":"validation","validationCode":"String[] pairs = /* ... */;\nif (pairs != null && pairs.length % 2 != 0) {\n    throw new IllegalArgumentException(\"toStringMap needs even number of args (key,value,...)\");\n}\nCollectionUtils.toStringMap(pairs);","typeGuard":"static boolean evenLength(String[] pairs) {\n    return pairs == null || pairs.length % 2 == 0;\n}","tryCatchPattern":null,"preventionTips":["Always pass complete key/value pairs to toStringMap.","When building from a list, assert even size first.","Prefer Map.of / Map.put for clarity when constructing small maps."],"tags":["collections","map","validation","varargs"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}