java
21 lines · 5 steps
Backfilling gaps in a daily time series in Java
Turn a sparse list of dated samples into a continuous day-by-day series by synthesizing entries for every missing date.
Explained by
highlit
1public List<DailySample> backfillMissingDates(List<DailySample> samples, double fillValue) {
2 if (samples.isEmpty()) {
3 return List.of();
4 }
5
6 Map<LocalDate, DailySample> byDate = samples.stream()
7 .collect(Collectors.toMap(
8 DailySample::date,
9 Function.identity(),
10 (a, b) -> b,
11 TreeMap::new));
12
13 LocalDate start = ((TreeMap<LocalDate, DailySample>) byDate).firstKey();
14 LocalDate end = ((TreeMap<LocalDate, DailySample>) byDate).lastKey();
15
16 return start.datesUntil(end.plusDays(1))
17 .map(date -> byDate.getOrDefault(date, new DailySample(date, fillValue)))
18 .collect(Collectors.toList());
19}
20
21record DailySample(LocalDate date, double value) {}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A sorted map lets you derive the full date range and do O(1) lookups in one structure.
- 2datesUntil generates a continuous sequence so you can materialize entries that never existed in the input.
- 3Guarding the empty case up front keeps later range calculations from throwing on missing keys.
Related explainers
java
public record AppConfig( String host, int port, String databaseUrl,
Loading typed config from env vars in Java
records
configuration
environment-variables
Intermediate
6 steps
java
public class RequestThrottler { private final Semaphore permits; private final long acquireTimeoutMillis;
Bounding concurrency with a Semaphore in Java
concurrency
semaphore
rate-limiting
Intermediate
6 steps
java
public final class Ulid { private static final char[] ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".toCharArray(); private static final SecureRandom RANDOM = new SecureRandom();
How to generate a ULID in Java
base32-encoding
bit-manipulation
identifiers
Intermediate
8 steps
java
@RestController @RequestMapping("/api/reports") public class ReportController {
Header-driven endpoints in a Spring controller
rest api
request headers
dependency injection
Intermediate
7 steps
java
public final class CaseConverter { private static final Pattern CAMEL_BOUNDARY = Pattern.compile("([a-z0-9])([A-Z])|([A-Z]+)([A-Z][a-z])");
Converting camelCase to snake_case in Java
regex
string-manipulation
utility-class
Intermediate
7 steps
java
@Component public class RegionCacheWarmer implements SmartInitializingSingleton { private static final Logger log = LoggerFactory.getLogger(RegionCacheWarmer.class);
Warming a Spring cache at startup
caching
startup-hook
dependency-injection
Intermediate
7 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/backfilling-gaps-in-a-daily-time-series-in-java-explained-java-9336/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.