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
import java.util.Comparator; import java.util.LinkedHashMap; import java.util.Map; import java.util.regex.Pattern;
Ranking the most frequent words in Java
streams
regex
grouping
Intermediate
7 steps
php
<?php namespace App\Reminders;
Scheduling reminders across time zones in PHP
timezones
datetime
immutability
Intermediate
7 steps
java
public record ServerConfig( String host, int port, Duration timeout,
Loading typed config into a Java record
records
factory-methods
parsing
Intermediate
7 steps
java
public final class IbanValidator { private static final Map<String, Integer> COUNTRY_LENGTHS = Map.ofEntries( Map.entry("DE", 22), Map.entry("FR", 27), Map.entry("GB", 22),
Validating IBANs with the mod-97 checksum
validation
checksum
modular-arithmetic
Intermediate
8 steps
java
@Configuration @EnableWebSecurity public class OAuth2SecurityConfig {
How OIDC login works in Spring Security
oauth2
openid-connect
authorization
Intermediate
8 steps
java
@RestController @RequestMapping("/api/reports") public class OrderReportController {
Streaming a CSV export in Spring
streaming
csv-export
backpressure
Intermediate
9 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.