Ehcache - using a ListInteger as the cache value
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Using a List<Integer> as an Ehcache value is perfectly valid, but there are two practical concerns: Java’s generic type erasure and value mutability. Ehcache can cache a list just fine, but your code still needs to choose the cache type carefully and decide whether callers are allowed to mutate the cached list after retrieval.
Declare the Cache With the Right Value Type
In Ehcache 3, the cache is declared with a key type and a value type. Because Java erases generic parameters at runtime, the runtime type is still List.class even though the application code treats the value as List<Integer>.
The cast is awkward, but it is a normal consequence of type erasure. At compile time your code is List<Integer>. At runtime the cache sees List.
Prefer Immutable Lists for Cached Values
A cache works best when values behave like snapshots. If a caller reads a list from the cache and then mutates it, other parts of the application may observe those changes unexpectedly.
List.copyOf makes the intent clear: the cached value should be read, not edited in place.
Think About Serialization Early
If the cache is heap-only, Ehcache stores normal Java objects in memory. If you later add off-heap or disk tiers, the values may need to be serialized. Integer is already serializable, so the main concern is whether the concrete list implementation is safe for that tier.
A cache that works on heap can still fail later if you switch to off-heap or disk without checking how the values are serialized.
Update Cached Lists by Replacing the Whole Value
Treat the list as a value object. Read the current list, build a new list with the update, and write the new list back.
This is easier to reason about than mutating a list instance that may already be shared in memory or serialized in another tier.
Common Pitfalls
- Assuming
List<Integer>is special when the runtime cache type is still justList.class. - Storing a mutable list and then modifying it after putting it into the cache.
- Forgetting that off-heap or disk tiers can introduce serialization requirements.
- Using
nullas a meaningful cached value when an empty immutable list would be clearer. - Caching tiny, cheap-to-compute lists that do not really benefit from a cache at all.
Summary
- Ehcache can store a
List<Integer>value without any unusual trick beyond the normalList.classruntime cast. - Prefer immutable lists so cached values behave like snapshots.
- Test serialization behavior if you use off-heap or disk tiers.
- Replace cached lists wholesale instead of mutating them in place.
- Cache collections only when they are actually expensive or frequently reused.
Related reading
- Ehcache Jgroups replication using TCP
- EHCache JMS Replication Node consistency tracking
- Ehcache Replicated Cache not synchronizing at startup
- Ehcache replicated cache RMI bootstrap
- Election Algorithms - A ring algorithm
- Element-wise addition of 2 lists?
- EHCache RMI Replication on JBoss/EC2 throws java.rmi.NoSuchObjectException no such object in table
- EJB 3.1 asynchronous method and thread pool

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.