HashMap.containsKey(Object key) 找不到键
当然我做错了什么,但无法意识到是什么:) 在我尝试以常见方式 keyValuePairs.containsKey("Name") 检查集合中是否存在特定键并返回“false”后,我用数据填充键/值对集合 Map keyValuePairs。但 keyValuePairs.keySet() 返回存在“Name”的键集。
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
}
}
}
匹配后输出结果:
Id=1;
Name=Til afskrivning;
LocationId=1;
Editable=true;
Default=true;
Transcribed=false;
"keyValuePairs"= HashMap (id=830062742672) { LocationId=1,默认=true,可编辑=true,名称=Til afskrivning,Id=1,转录=false}
“keyValuePairs.keySet()”= HashMap$1 (id=830062763448)
[位置Id、默认、可编辑、名称、Id、转录]"keyValuePairs.containsKey("名称")"= false
有人可以解释一下它有什么问题吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用您在问题中提供的信息:
某些键名称前面的额外空格表明插入的键是
" Name"
(注意前面的空格)。如果您提供有关正则表达式的更多信息,我们也许能够找出发生这种情况的原因。您还可以通过记录/打印
group(1)
和group(2)
匹配的内容来自行调试;我相信您会发现它与额外的空格相匹配。一个快速修复方法是将
group(1).trim()
和group(2).trim()
放入地图中,但更好的选择是修复正则表达式。Quoting the information you gave in the question:
The extra spaces in front of some of the key names suggest that the key inserted was
" Name"
(note the preceding space). If you give more information about the regex, we may be able to figure out why this happened.You can also debug this yourself by logging/printing what
group(1)
andgroup(2)
matches; I'm sure you'll find that it matches the extra whitespaces.A quick fix is to put
group(1).trim()
andgroup(2).trim()
into the map instead, but the better option is to fix the regex.