The given code defines a Java method called `extractKeyValues` that...
September 2, 2025 at 03:27 PM
The given code defines a Java method called extractKeyValues
that extracts specific key-value pairs from a collection of lines of text. Here's what it does, step by step:
Function Inputs:
lines
: An array of strings, which represents the text input where the extraction will be performed. Each string in the array is treated as a line.keySpec
: A specification of the keys to extract, represented as a single string in a specific format (e.g.,"customer_id:string-order_id:number"
). Each key name is specified alongside its type (though the type is ignored in this function).
Function Outputs:
- A
Map<String, String>
where the keys are uppercase versions of key names (fromkeySpec
), and the values are the corresponding extracted string values from the inputlines
. - If a key's value cannot be found in the
lines
, a message is printed to indicate this.
Key Steps in the Code:
-
Early Exit for Empty
keySpec
:- If
keySpec
is null or blank, an emptyLinkedHashMap
is returned immediately.
- If
-
Extract Key Names from
keySpec
:- The
keySpec
string is parsed to extract key names. For example, the specification"customer_id:string-order_id:number"
is split into parts using"-"
as a delimiter. Each part is then split further with":"
to isolate the key name (ignoring the type). These key names are added to aSet
(keyNames
) to ensure uniqueness.
- The
-
Search and Extract Values:
- For every key name in
keyNames
, the method looks for a line inlines
that matches the pattern of that key. It assumes that valid keys are followed by a JSON-like structure, such as:"customer_id": { "#text": "12345" }
- Specifically, the method:
- Searches for a line that matches the key name (e.g.,
"customer_id": {
). - If such a line is found, it looks at the next few lines (up to 4) for a line containing
"#text"
, which is assumed to hold the actual value. - The value is extracted using the helper method
extractBetweenQuotes
, which retrieves the string enclosed in quotes. - The extracted value is added to the
keys
map with the key name in uppercase as the map key.
- Searches for a line that matches the key name (e.g.,
- For every key name in
-
Handle Missing Keys:
- If a key name is not found in the input
lines
, an error message is printed to indicate that the value for the key could not be located.
- If a key name is not found in the input
-
Return Results:
- Finally, the method returns the
keys
map containing the extracted key-value pairs.
- Finally, the method returns the
Key Features:
- The method assumes the input follows a JSON-like structure for keys and values.
- Keys are case-insensitively processed (converted to upper case).
- Logging is used to inform the caller when a key's value could not be found.
Example:
Input:
String[] lines = {
"\"customer_id\": {",
" \"#text\": \"12345\"",
"}",
"\"order_id\": {",
" \"#text\": \"67890\"",
"}"
};
String keySpec = "customer_id:string-order_id:number";
Output:
The returned map would be:
{ "CUSTOMER_ID" -> "12345", "ORDER_ID" -> "67890" }
Notes:
- If
keySpec
doesn't contain any valid keys or if the keys cannot be located inlines
, the map remains empty or partially filled, with corresponding error messages printed. - The helper method
extractBetweenQuotes
is critical, but its implementation is not provided. Likely, it extracts a substring surrounded by double quotes ("
).
Summary:
The extractKeyValues
method extracts key-value pairs from a structured text (passed as an array of lines) based on a provided key specification string. It is designed to work with JSON-like data and supports logging for missing keys.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node