TheAlgorithms/Java · error · IllegalArgumentException
Number of hash functions and bit array size must be greater
Error message
Number of hash functions and bit array size must be greater than 0
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java:38
private final int numberOfHashFunctions;
private final BitSet bitArray;
private final Hash<T>[] hashFunctions;
/**
* Constructs a BloomFilter with a specified number of hash functions and bit
* array size.
*
* @param numberOfHashFunctions the number of hash functions to use
* @param bitArraySize the size of the bit array, which determines the
* capacity of the filter
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is
* less than 1
*/
@SuppressWarnings("unchecked")
public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
if (numberOfHashFunctions < 1 || bitArraySize < 1) {
throw new IllegalArgumentException("Number of hash functions and bit array size must be greater than 0");
}
this.numberOfHashFunctions = numberOfHashFunctions;
this.bitArray = new BitSet(bitArraySize);
this.hashFunctions = new Hash[numberOfHashFunctions];
initializeHashFunctions();
}
/**
* Initializes the hash functions with unique indices to ensure different
* hashing.
*/
private void initializeHashFunctions() {
for (int i = 0; i < numberOfHashFunctions; i++) {
hashFunctions[i] = new Hash<>(i);
}
}
/**View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before int k = optimalHashes(n, p); // can be 0 new BloomFilter<>(k, m); // after int k = Math.max(1, optimalHashes(n, p)); int m = Math.max(1, optimalBits(n, p)); new BloomFilter<>(k, m);
Defensive patterns
Strategy: validation
Validate before calling
static BloomFilter<T> createBloomFilter(int hashCount, int bitSize) {
if (hashCount < 1) throw new IllegalArgumentException("hashCount must be >= 1, got " + hashCount);
if (bitSize < 1) throw new IllegalArgumentException("bitSize must be >= 1, got " + bitSize);
return new BloomFilter<>(hashCount, bitSize);
} Try / catch
try {
return new BloomFilter<>(k, m);
} catch (IllegalArgumentException e) {
throw new ConfigException("BloomFilter requires hashCount>=1 and bitSize>=1; got k=" + k + ", m=" + m);
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Buffer size must be positive
- Invalid unit '{}'. Supported units are: {}
- inputUnit must be different from outputUnit.
- No more elements in the bag.
- Null items are not allowed
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/66e4d8dc6f22cfce.
Report an issue: GitHub.