A fixed window needs no windowing library: integer-divide the timestamp by the window width and that quotient IS the window id. A TreeMap downstream keeps the windows in clock order.
record Event(int at, String kind) {}
var events = List.of(new Event(2, "ok"), new Event(7, "ok"), new Event(11, "err"),
new Event(13, "ok"), new Event(26, "err"), new Event(28, "ok"));
int window = 10;
Map<Integer, Long> perWindow = events.stream()
.collect(Collectors.groupingBy(e -> e.at() / window, TreeMap::new, Collectors.counting()));
perWindow.forEach((w, n) ->
System.out.println("[" + (w * window) + "-" + (w * window + window - 1) + "s] " + n));
[0-9s] 2
[10-19s] 2
[20-29s] 2
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-20