Quick revision
The short answer
We define the XML shape as two small classes, StockXml with @XmlElement(name = "item") List<ItemXml> items and ItemXml with @XmlAttribute sku and qty, and write StockAdapter extends XmlAdapter<StockXml, Map<String, Integer>> that turns map entries into items and back. Annotating the map field with @XmlJavaTypeAdapter(StockAdapter.class) makes JAXB write <stock><item sku="A1" qty="5"/></stock>. We use a LinkedHashMap when item order matters, because JAXB's default map mapping produces an entry, key and value structure that rarely matches any partner schema.
What to remember
In this answer, we saw that JAXB's default map mapping writes entry, key and value elements, which partner schemas rarely use. We described the wanted shape with StockXml and ItemXml, wrote StockAdapter extending XmlAdapter<StockXml, Map<String, Integer>> to convert in both directions, and applied it with @XmlJavaTypeAdapter on the map field. We also handled the details that matter in production: LinkedHashMap for stable order, explicit rejection of duplicate SKUs, and missing quantities.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We define the XML shape as two small classes, StockXml with @XmlElement(name = "item") List<ItemXml> items and ItemXml with @XmlAttribute sku and qty, and write StockAdapter extends XmlAdapter<StockXml, Map<String, Integer>> that turns map entries into items and back. Annotating the map field with @XmlJavaTypeAdapter(StockAdapter.class) makes JAXB write <stock><item sku="A1" qty="5"/></stock>. We use a LinkedHashMap when item order matters, because JAXB's default map mapping produces an entry, key and value structure that rarely matches any partner schema.
Why this question is asked
Maps are natural in Java code and awkward in XML, because XML Schema has no map type. Every partner format represents a map in its own way, usually as a list of elements with a key attribute. Interviewers ask for this adapter because it is a realistic integration task and because it shows we understand adapters as a general tool: describe the XML as plain classes, then convert. In this answer, we will see what JAXB does with a map by default, design the XML classes, write the adapter, apply it, and handle order, duplicates and nulls.
1What JAXB Does With a Map by Default
JAXB does support Map properties, but with a fixed shape of its own: an element per entry, each with a key and a value child element. That shape is not what the partner wants, and we cannot rename its parts with the usual annotations.
So we take control with an adapter. The idea of every XmlAdapter is the same: describe the XML we want with simple classes that JAXB maps naturally, then convert between those classes and the Java type we really want to use.
2Describing the XML With Two Small Classes
The wanted XML is a stock element holding repeated item elements, and each item has two attributes. That is a class with a list, and a class with two attribute fields.
These classes are deliberately plain: public fields, no-argument constructors and no logic. They exist only to describe the XML, and no code outside the adapter uses them.
3Writing the Adapter
XmlAdapter<StockXml, Map<String, Integer>> converts in both directions. marshal() turns each map entry into an ItemXml; unmarshal() turns each item back into a map entry.
7 messages in order. Play them, or step through them one at a time.
JAXB maps StockXml naturally; the adapter turns it into the map we use.
4Applying the Adapter
We annotate the map field with @XmlJavaTypeAdapter. The element name comes from the field, or from @XmlElement(name) if we want a different one.
Notice that our class still holds a plain Map<String, Integer>, so all the code that uses stock keeps its natural Java form, while the XML matches the partner exactly.
5Order, Duplicates and Nulls
Three details decide whether the adapter is correct in production. Order: a HashMap iterates in an order that depends on hashing, so the items in the output would come out in an unpredictable order. If the partner cares, or if we compare files in tests, we use a LinkedHashMap, which keeps insertion order, or a TreeMap for sorted SKUs. The adapter's unmarshal() returns a LinkedHashMap so document order survives a round trip.
Duplicates: XML can contain the same sku twice, and a map cannot. Map.put() would silently keep the last one, which could hide a data problem. So the adapter rejects duplicates explicitly; if the partner's rules say quantities should be summed instead, we use merge(sku, qty, Integer::sum) and document that.
Nulls: qty is an int in ItemXml, so a missing qty attribute becomes 0, which may be wrong. If a missing quantity must be detected, we make it an Integer and check for null, or validate the document against the schema, where required="true" on the attribute makes the missing value an error. A map value that is null in Java cannot be written as an int attribute, so marshal() should reject or skip it deliberately.
6Follow-up questions
Why does the adapter use StockXml rather than List<ItemXml> as its value type?
Because the XML has a container element, <stock>, and the value type must describe a whole element. StockXml is that element, holding the repeated items. An adapter cannot produce a bare list as an element.
Could I write one generic adapter for all maps?
Partly: a generic MapAdapter with entry elements is possible, but the element and attribute names are usually specific to each partner, and annotations cannot be parameterised. In practice one small adapter per shape is clearer and easy to test.
Does this work with xjc-generated classes?
Generated classes describe the schema, so they would already contain a list of items. We would keep them as they are and convert the list to a map in the domain mapping layer, or tell xjc in a binding file to apply our adapter to that property.
7Conclusion
In this answer, we saw that JAXB's default map mapping writes entry, key and value elements, which partner schemas rarely use. We described the wanted shape with StockXml and ItemXml, wrote StockAdapter extending XmlAdapter<StockXml, Map<String, Integer>> to convert in both directions, and applied it with @XmlJavaTypeAdapter on the map field. We also handled the details that matter in production: LinkedHashMap for stable order, explicit rejection of duplicate SKUs, and missing quantities.
Open with
JAXB maps a Map with its own entry, key and value structure, which almost never matches a partner's schema, so I would use an adapter. First I describe the XML I want as plain classes: StockXml, which has a list of ItemXml annotated with @XmlElement name item, and ItemXml, which has two @XmlAttribute fields, sku and qty, plus a no-argument constructor.
Then, if they want more
Then StockAdapter extends XmlAdapter of StockXml and Map of String to Integer. marshal goes through the map entries and adds one ItemXml per entry; unmarshal goes through the items and puts them into a LinkedHashMap so the document order is kept.
I annotate the map field in Warehouse with @XmlJavaTypeAdapter of StockAdapter, and @XmlElement name stock if I need to rename it, so the class keeps a plain Map while the XML is stock with item elements.
A few details I would handle on purpose: a LinkedHashMap or TreeMap for a stable order, since a HashMap's order is unpredictable; duplicate SKUs, which I reject rather than silently overwrite, unless the contract says to sum them; and a missing qty, which an int would turn into 0, so I'd use Integer or validate against the schema. I would test the adapter directly and with a round trip.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "JAXB cannot map Map<String, Integer> to <stock><item sku="A1" qty="5"/></stock>...": https://scrivio-interview-library.netlify.app/java/jaxb/40f43e87b5/