java
31 lines · 7 steps
Flattening nested maps into dotted keys
A recursive walk turns nested maps and lists into a flat map whose keys encode the full path to each value.
Explained by
highlit
1public final class MapFlattener {
2
3 private MapFlattener() {
4 }
5
6 public static Map<String, Object> flatten(Map<String, Object> source) {
7 Map<String, Object> result = new LinkedHashMap<>();
8 flattenInto(null, source, result);
9 return result;
10 }
11
12 private static void flattenInto(String prefix, Map<String, Object> source, Map<String, Object> target) {
13 for (Map.Entry<String, Object> entry : source.entrySet()) {
14 String key = prefix == null ? entry.getKey() : prefix + "." + entry.getKey();
15 appendValue(key, entry.getValue(), target);
16 }
17 }
18
19 @SuppressWarnings("unchecked")
20 private static void appendValue(String key, Object value, Map<String, Object> target) {
21 if (value instanceof Map<?, ?> nested) {
22 flattenInto(key, (Map<String, Object>) nested, target);
23 } else if (value instanceof List<?> list) {
24 for (int i = 0; i < list.size(); i++) {
25 appendValue(key + "[" + i + "]", list.get(i), target);
26 }
27 } else {
28 target.put(key, value);
29 }
30 }
31}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Building a path prefix as you descend lets you express deep structure as flat, self-describing keys.
- 2Two mutually recursive helpers — one for maps, one for individual values — cleanly separate iteration from type dispatch.
- 3A LinkedHashMap preserves the insertion order of the flattened output, keeping keys in a predictable, readable sequence.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 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/flattening-nested-maps-into-dotted-keys-explained-java-b0df/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.