java
31 lines · 6 steps
A thread-safe memoized factorial cache
Caching factorial results in a ConcurrentHashMap so repeated calls skip the BigInteger arithmetic.
Explained by
highlit
1import java.util.Map;
2import java.util.concurrent.ConcurrentHashMap;
3import java.math.BigInteger;
4
5public class FactorialCache {
6
7 private final Map<Integer, BigInteger> cache = new ConcurrentHashMap<>();
8
9 public BigInteger factorial(int n) {
10 if (n < 0) {
11 throw new IllegalArgumentException("n must be non-negative: " + n);
12 }
13 return cache.computeIfAbsent(n, this::compute);
14 }
15
16 private BigInteger compute(int n) {
17 BigInteger result = BigInteger.ONE;
18 for (int i = 2; i <= n; i++) {
19 result = result.multiply(BigInteger.valueOf(i));
20 }
21 return result;
22 }
23
24 public int cachedEntries() {
25 return cache.size();
26 }
27
28 public void evict(int n) {
29 cache.remove(n);
30 }
31}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1computeIfAbsent turns a map into a memoization table without manual check-then-put races.
- 2ConcurrentHashMap lets multiple threads share a cache safely with no explicit locking.
- 3BigInteger keeps factorial results exact even when they overflow primitive integer types.
Related explainers
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
java
public class SortedListMerger { public static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length];
Merging two sorted arrays in Java
two-pointers
merging
arrays
Beginner
6 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
java
import java.util.ArrayDeque; import java.util.Deque; public final class RollingAverage {
A rolling average over a sliding window
sliding-window
running-sum
deque
Intermediate
7 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-thread-safe-memoized-factorial-cache-explained-java-4bfc/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.