{"record":{"id":"0c6bda2d283d26df","repo":"apache/dubbo","slug":"map-pairs-can-not-be-odd-number-0c6bda","errorCode":null,"errorMessage":"Map pairs can not be odd number.","messagePattern":"Map pairs can not be odd number\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java","lineNumber":233,"sourceCode":"            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        }\n        int len = pairs.length / 2;\n        for (int i = 0; i < len; i++) {\n            ret.put((K) pairs[2 * i], (V) pairs[2 * i + 1]);\n        }\n        return ret;\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public static <K, V> Map<K, V> objToMap(Object object) throws IllegalAccessException {\n        Map<K, V> ret = new HashMap<>();\n        if (object != null) {\n            Field[] fields = object.getClass().getDeclaredFields();\n            for (Field field : fields) {\n                field.setAccessible(true);\n                Object value = field.get(object);\n                if (value != null) {\n                    ret.put((K) field.getName(), (V) value);","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java#L215-L251","documentation":"Thrown by CollectionUtils.toMap when the varargs 'pairs' array has an odd length. Like toStringMap, toMap interprets arguments as alternating key,value pairs; an odd count means an unpaired key with no value, which cannot form a well-defined Map. Null/empty arrays return an empty map and do not throw.","triggerScenarios":"CollectionUtils.toMap(pairs...) where pairs.length % 2 != 0 — e.g. toMap(1,\"a\",2) (3 args). The method casts elements unchecked to K and V, but the parity check runs first.","commonSituations":"Constructing a small map inline with an odd number of literals; a trailing element from a list spread with no partner; refactoring that drops one half of a pair. The unchecked casts (K/V) also mean type errors surface later as ClassCastException, but parity fails fast here.","solutions":["Supply arguments in complete key/value pairs — add the missing partner or remove the orphan.","When building from a collection, assert even size before spreading.","Prefer Map.of(...) (Java 9+) for small literal maps to avoid parity mistakes entirely."],"exampleFix":"// before\nMap<Integer,String> m = CollectionUtils.toMap(1,\"a\",2); // throws\n\n// after\nMap<Integer,String> m = CollectionUtils.toMap(1,\"a\",2,\"b\");\n// or Java 9+:\nMap<Integer,String> m = Map.of(1,\"a\",2,\"b\");","handlingStrategy":"validation","validationCode":"Object[] pairs = /* ... */;\nif (pairs != null && pairs.length % 2 != 0) {\n    throw new IllegalArgumentException(\"toMap needs even number of args (key,value,...)\");\n}\nCollectionUtils.toMap(pairs);","typeGuard":"static boolean evenLength(Object[] pairs) {\n    return pairs == null || pairs.length % 2 == 0;\n}","tryCatchPattern":null,"preventionTips":["Supply arguments in complete key/value pairs.","Use Map.of (Java 9+) for literal maps to avoid parity mistakes.","Assert even size when spreading a dynamic list into toMap."],"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"}