java - How to parse string key/value and make a new key/value out of it and load it in same map? -
i have map of string , string in have key/value pair shown below. each client_123
, have 2 keys values shown below.
test_client_123=7|0.1|0.2|0.3|0.3 test_abc_pqr_client_123=16|5.5562501|5.1999998|13.6000004|13.6000004 test_client_987=9|0.3|0.4|0.7|0.7 test_abc_pqr_client_987=10|2.222|3.333|4.567|7.876
this example, have many more key/value pairs in same format. difference client_123
other clients client_543
, after =
numbers might different in pipe well. that's all.
what each value mean in pipe delimited format: here 7 count, 0.1 average in ms, 0.2 median in ms, 0.3 95th percentile in ms , 0.3 again 99th percentile in ms. second line well. , in format always.
problem statement:
for line test_client_123=7|0.1|0.2|0.3|0.3
, need make below new key/value pair , load in same map:
test_in_process_client_123_count=7 test_in_process_client_123_avg_in_ms=0.1 test_in_process_client_123_median_in_ms=0.2 test_in_process_client_123_95_in_ms=0.3 test_in_process_client_123_99_in_ms=0.3
and line test_abc_pqr_client_123=16|5.5562501|5.1999998|13.6000004|13.6000004
, want make below new key/value pair , load in same map:
test_abc_pqr_client_123_count=16 test_abc_pqr_client_123_avg_in_ms=5.5562501 test_abc_pqr_client_123_median_in_ms=5.1999998 test_abc_pqr_client_123_95_in_ms=13.6000004 test_abc_pqr_client_123_99_in_ms=13.6000004
how can achieve this? below code load original key/value pairs in map:
string response = resttemplate.getforobject(url, string.class); matcher m = pattern.matcher(response); while (m.find()) { metricholder.put(m.group(1), m.group(2)); }
now metricholder
map have above original key , pipe delimited values. want load new key/value pair in same metricholder
map , remove original key/value pair map after convert them new key/value pair.
interesting problem solve, came out own solution this, hope helps or useful trying do, if understand want add new records map, based on previous entries, did:
public class dictionaryprocessor { //suffixes , common string parts final static string process = "_in_process_"; final static string count = "_count"; final static string avg_in_ms = "_avg_in_ms"; final static string median_in_ms = "_median_in_ms"; final static string n95_in_ms = "_95_in_ms"; final static string n99_in_ms = "_99_in_ms"; final static string pqr = "_pqr_"; public static void main(string[] args) { //assuming data have: map<string, string> dictionarymap = new hashmap<string, string>(); dictionarymap.put("test_client_123", "7|0.1|0.2|0.3|0.3"); dictionarymap.put("test_abc_pqr_client_123", "16|5.5562501|5.1999998|13.6000004|13.6000004"); dictionarymap.put("test_client_987", "9|0.3|0.4|0.7|0.7"); dictionarymap.put("test_abc_pqr_client_987", "10|2.222|3.333|4.567|7.876"); //i define temporary map hold new values map<string, string> tempmap = new hashmap<string, string>(); //we iterate our map (map.entry<string, string> entry : dictionarymap.entryset()) { string currentkey = entry.getkey();//get current key string currentvalue = entry.getvalue();//get current value key //since value has data separated "|", i'd use "stringtokenizer" (split or regex worth use to) stringtokenizer tokenizer = new stringtokenizer(currentvalue, "|"); string count = tokenizer.nexttoken().trim(); string avgdata = tokenizer.nexttoken().trim(); string mediandata = tokenizer.nexttoken().trim(); string n95data = tokenizer.nexttoken().trim(); string n99data = tokenizer.nexttoken().trim(); tempmap.put(generatenewkey(currentkey, currentkey.contains(pqr), count), count); tempmap.put(generatenewkey(currentkey, currentkey.contains(pqr), avg_in_ms), avgdata); tempmap.put(generatenewkey(currentkey, currentkey.contains(pqr), median_in_ms), mediandata); tempmap.put(generatenewkey(currentkey, currentkey.contains(pqr), n95_in_ms), n95data); tempmap.put(generatenewkey(currentkey, currentkey.contains(pqr), n99_in_ms), n99data); } //iterate temporary map , add existing dictionary map (map.entry<string, string> entry : tempmap.entryset()) { dictionarymap.put(entry.getkey(), entry.getvalue()); } printmap(dictionarymap); } private static void printmap(map<string, string> targetmap) { (map.entry<string, string> entry : targetmap.entryset()) { system.out.println(entry.getkey() + "=" + entry.getvalue()); } } private static string generatenewkey(string currentkey, boolean haspqr, string constant) { stringbuilder newkey = new stringbuilder(); if (haspqr) { newkey.append(currentkey).append(constant); } else { string firstpart = currentkey.substring(0, currentkey.indexof("_")); string secondpart = currentkey.substring(currentkey.lastindexof("_") + 1, currentkey.length()); newkey.append(firstpart).append(process).append(secondpart).append(constant); } return newkey.tostring(); } }
hope useful. happy coding :)
Comments
Post a Comment