TheAlgorithms/Java · error · IllegalArgumentException
Capacity must be greater than zero.
Error message
Capacity must be greater than zero.
What it means
Thrown by the LFUCache(int capacity) constructor when capacity <= 0. LFU eviction depends on a frequency-ordered linked list whose invariants assume at least one slot; a zero capacity would make the cache unusable and every put an immediate eviction. The no-arg constructor delegates with DEFAULT_CAPACITY, so this only fires on an explicit bad capacity.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java:75
private final int capacity;
private static final int DEFAULT_CAPACITY = 100;
/**
* Constructs an LFU cache with the default capacity.
*/
public LFUCache() {
this(DEFAULT_CAPACITY);
}
/**
* Constructs an LFU cache with the specified capacity.
*
* @param capacity The maximum number of items that the cache can hold.
* @throws IllegalArgumentException if the specified capacity is less than or equal to zero.
*/
public LFUCache(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity must be greater than zero.");
}
this.capacity = capacity;
this.cache = new HashMap<>();
}
/**
* Retrieves the value associated with the given key from the cache.
* If the key exists, the node's frequency is incremented, and the node is repositioned
* in the linked list based on its updated frequency.
*
* @param key The key whose associated value is to be returned.
* @return The value associated with the key, or {@code null} if the key is not present in the cache.
*/
public V get(K key) {
Node node = cache.get(key);
if (node == null) {
return null;
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Default to a positive capacity when config is absent.
- Validate config at startup: if (cap <= 0) throw with a clear message.
- Use the no-arg constructor new LFUCache<>() to get the default capacity.
Example fix
// before new LFUCache<>(config.getMaxEntries()) // after int cap = config.getMaxEntries(); if (cap <= 0) cap = 1000; new LFUCache<>(cap)
Defensive patterns
Strategy: validation
Validate before calling
int cap = configuredCapacity; if (cap <= 0) cap = DEFAULT_CAPACITY; new LFUCache<>(cap);
Type guard
static boolean isValidCapacity(int capacity) {
return capacity > 0;
} Prevention
- Use the no-arg constructor new LFUCache<>() to get the default capacity.
- Default optional capacity config to a positive constant.
- Guard capacity math against underflow to zero.
When it happens
Trigger: new LFUCache<>(0); new LFUCache<>(-1); capacity sourced from config that defaulted to 0 or was computed as size/batch with a 0 divisor.
Common situations: Optional config defaulting to 0; capacity derived from memory math that underflows; tests asserting behavior at capacity 0.
Related errors
- Capacity must be > 0
- Capacity must be > 0
- Key must not be null
- Key and value must not be null
- TTL must be >= 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/08788461e44abf71.
Report an issue: GitHub.