java
41 lines · 7 steps
A case-insensitive HTTP headers map in Java
Storing HTTP headers in a case-insensitive TreeMap of value lists gives clean multi-value accessors and safe reads.
Explained by
highlit
1public final class HttpHeaders {
2
3 private final NavigableMap<String, List<String>> values =
4 new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
5
6 public HttpHeaders add(String name, String value) {
7 values.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
8 return this;
9 }
10
11 public HttpHeaders set(String name, String value) {
12 List<String> list = new ArrayList<>(1);
13 list.add(value);
14 values.put(name, list);
15 return this;
16 }
17
18 public Optional<String> first(String name) {
19 List<String> list = values.get(name);
20 return (list == null || list.isEmpty())
21 ? Optional.empty()
22 : Optional.of(list.get(0));
23 }
24
25 public List<String> all(String name) {
26 List<String> list = values.get(name);
27 return list == null ? List.of() : Collections.unmodifiableList(list);
28 }
29
30 public boolean contains(String name) {
31 return values.containsKey(name);
32 }
33
34 public List<String> remove(String name) {
35 return values.remove(name);
36 }
37
38 public Set<String> names() {
39 return Collections.unmodifiableSet(values.keySet());
40 }
41}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A TreeMap seeded with CASE_INSENSITIVE_ORDER makes lookups match headers regardless of letter casing.
- 2Returning this from mutators enables a fluent chained-call API.
- 3Wrapping reads in Optional and unmodifiable collections shields callers from nulls and accidental mutation.
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
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
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 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/a-case-insensitive-http-headers-map-in-java-explained-java-8c63/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.