Is there a simple way of parsing this text into a Map
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Yes, if the text really follows a simple key-value structure. The hard part is usually not splitting strings. It is deciding the parsing rules up front so blank lines, comments, duplicate keys, and malformed entries are handled predictably instead of only on happy-path input.
Define the Input Contract First
Before writing parsing code, decide what each line means. A simple line-based format often uses rules like these:
- one key-value pair per line
- the first
=separates key from value - blank lines are ignored
- lines starting with
#are comments - duplicate keys either overwrite or raise an error
Without those rules, the parser is just guessing.
A Small Java Parser
The following parser reads key=value text, ignores blank lines and comments, and records malformed lines separately from the parsed map.
This keeps syntax parsing simple while still giving the caller enough information to react to bad lines.
Split on the First Delimiter Only
One of the easiest bugs is splitting on every = instead of only the first one. Values sometimes contain the delimiter themselves.
The key should be connection, and everything after the first delimiter should remain in the value. That is why indexOf plus substring is often better than a naive split("=").
Example Usage
This separation between parsed values and parse errors is often more useful than throwing immediately, because different callers may want different error-handling policies.
Decide How to Handle Duplicate Keys
Duplicate keys are common in hand-edited files, so you should choose a policy intentionally.
Common choices are:
- last value wins
- first value wins
- duplicates are errors
The example parser uses “last value wins” because Map.put overwrites the old value. That is fine for override-style configuration, but if ambiguity is dangerous in your domain, you should detect duplicates and reject them.
Know When the Format Is No Longer Simple
A tiny custom parser is fine for a tiny format. It stops being fine when the text starts needing:
- quoting
- escaping
- nested data
- arrays
- multiline values
At that point, a standard format such as JSON, YAML, or TOML is usually safer than extending an ad hoc parser indefinitely.
Common Pitfalls
The most common mistake is writing parsing code before defining what counts as a valid line.
Another pitfall is splitting on every delimiter and corrupting values that legitimately contain =. Developers also often mix syntax parsing and business validation into one step, which makes the code harder to debug and evolve.
Finally, do not assume trim() is always correct for every format. Some input formats treat leading or trailing spaces inside values as meaningful.
Summary
- Parsing text into a
Mapis straightforward when the input contract is explicit. - Handle blanks, comments, malformed lines, and duplicates deliberately.
- Split on the first delimiter only.
- Keep syntax parsing separate from business validation.
- Move to a standard structured format once the text stops being truly simple.
Related reading
- Is there a simple way to delete a list element by value?
- Is there a simple way to delete a list element by value?
- Is there a tree structure or algorithm to shuffle around levels in a tree?
- Is there a true single-pair shortest path algorithm?
- Is there a way in kubectl patch to delete a specific object in an array without specifying the index?
- Is there a way to dump a stack trace without throwing an exception in java?
- Is there a way to iterate over a dictionary?
- Is there a way to measure how sorted a list is?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.