Sure I do something wrong, but can't realize what :) I populate a key/value pair collection Map keyValuePairs with data, after I try to check collection for specific key existence in a common way keyValuePairs.containsKey("Name") and get back "false". But keyValuePairs.keySet() gives back set of keys where "Name" exists.
public static void parseBusinessObject(String input, Object output) {
Class parsingObject = output.getClass();
Field[] fields = parsingObject.getDeclaredFields();
Pattern pattern = Pattern.compile("([^{=;]*)=([^;}]*);");
Matcher matcher = pattern.matcher(/*input*/"anyType{Id=1; Name=Til afskrivning; LocationId=1; Editable=true; Default=true; Transcribed=false; }");
Map<String, String> keyValuePairs = new HashMap<String, String>();
while (matcher.find()) {
if(!keyValuePairs.containsKey(matcher.group(1))){
keyValuePairs.put(matcher.group(1).trim(), matcher.group(2).trim());
}
}
for (Field field : fields) {
if(keyValuePairs.containsKey(field.getName())){
//TODO: add values to fields
}
}
}
output result after matching:
Id=1;
Name=Til afskrivning;
LocationId=1;
Editable=true;
Default=true;
Transcribed=false;
"keyValuePairs"= HashMap (id=830062742672) { LocationId=1, Default=true, Editable=true, Name=Til afskrivning, Id=1, Transcribed=false}
"keyValuePairs.keySet()"= HashMap$1 (id=830062763448)
[ LocationId, Default, Editable, Name, Id, Transcribed]"keyValuePairs.containsKey("Name")"= false
Could anybody please explain me what's wrong with it? Thank you.