{"record":{"id":"66e4d8dc6f22cfce","repo":"TheAlgorithms/Java","slug":"number-of-hash-functions-and-bit-array-size-must-b","errorCode":null,"errorMessage":"Number of hash functions and bit array size must be greater than 0","messagePattern":"Number of hash functions and bit array size must be greater than 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java","lineNumber":38,"sourceCode":"\n    private final int numberOfHashFunctions;\n    private final BitSet bitArray;\n    private final Hash<T>[] hashFunctions;\n\n    /**\n     * Constructs a BloomFilter with a specified number of hash functions and bit\n     * array size.\n     *\n     * @param numberOfHashFunctions the number of hash functions to use\n     * @param bitArraySize          the size of the bit array, which determines the\n     *                              capacity of the filter\n     * @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is\n     *                                  less than 1\n     */\n    @SuppressWarnings(\"unchecked\")\n    public BloomFilter(int numberOfHashFunctions, int bitArraySize) {\n        if (numberOfHashFunctions < 1 || bitArraySize < 1) {\n            throw new IllegalArgumentException(\"Number of hash functions and bit array size must be greater than 0\");\n        }\n        this.numberOfHashFunctions = numberOfHashFunctions;\n        this.bitArray = new BitSet(bitArraySize);\n        this.hashFunctions = new Hash[numberOfHashFunctions];\n        initializeHashFunctions();\n    }\n\n    /**\n     * Initializes the hash functions with unique indices to ensure different\n     * hashing.\n     */\n    private void initializeHashFunctions() {\n        for (int i = 0; i < numberOfHashFunctions; i++) {\n            hashFunctions[i] = new Hash<>(i);\n        }\n    }\n\n    /**","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java#L20-L56","documentation":"Thrown by the BloomFilter constructor when numberOfHashFunctions < 1 or bitArraySize < 1. Both parameters must be positive integers because the filter allocates a BitSet of the given size and an array of that many hash functions; zero or negative values are structurally invalid.","triggerScenarios":"new BloomFilter<>(0, 1000), new BloomFilter<>(5, 0), new BloomFilter<>(-1, 100), new BloomFilter<>(3, -50). Often the values come from a computed config (e.g., optimal hash count formula that underflows for tiny expected insertions).","commonSituations":"Computing optimal parameters from formulas that can yield 0 for very small or zero expected element counts; configuration files with missing/zeroed values; integer underflow in size calculations; passing a user-provided capacity without clamping.","solutions":["Clamp computed values to a minimum of 1 before construction.","Validate config values at load time and reject non-positive entries.","Use a well-tested Bloom filter sizing formula and guard its lower bound."],"exampleFix":"// before\nint k = optimalHashes(n, p); // can be 0\nnew BloomFilter<>(k, m);\n// after\nint k = Math.max(1, optimalHashes(n, p));\nint m = Math.max(1, optimalBits(n, p));\nnew BloomFilter<>(k, m);","handlingStrategy":"validation","validationCode":"static BloomFilter<T> createBloomFilter(int hashCount, int bitSize) {\n    if (hashCount < 1) throw new IllegalArgumentException(\"hashCount must be >= 1, got \" + hashCount);\n    if (bitSize < 1) throw new IllegalArgumentException(\"bitSize must be >= 1, got \" + bitSize);\n    return new BloomFilter<>(hashCount, bitSize);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return new BloomFilter<>(k, m);\n} catch (IllegalArgumentException e) {\n    throw new ConfigException(\"BloomFilter requires hashCount>=1 and bitSize>=1; got k=\" + k + \", m=\" + m);\n}","preventionTips":["Clamp computed sizing parameters to a minimum of 1.","Validate config values at load time before passing to the constructor.","Use a tested Bloom filter sizing library/formula and guard lower bounds."],"tags":["java","datastructures","bloom-filter","construction","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}