HashMap.containsKey(Object key) 找不到键

发布于 2024-09-08 12:02:47 字数 1389 浏览 4 评论 0原文

当然我做错了什么,但无法意识到是什么:) 在我尝试以常见方式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-09-15 12:02:47

引用您在问题中提供的信息:

"keyValuePairs.keySet()"= HashMap$1  (id=830062763448)  
[ LocationId,  Default,  Editable,  Name, Id,  Transcribed]

某些键名称前面的额外空格表明插入的键是 " Name" (注意前面的空格)。如果您提供有关正则表达式的更多信息,我们也许能够找出发生这种情况的原因。

您还可以通过记录/打印 group(1)group(2) 匹配的内容来自行调试;我相信您会发现它与额外的空格相匹配。

一个快速修复方法是将 group(1).trim()group(2).trim() 放入地图中,但更好的选择是修复正则表达式。

Quoting the information you gave in the question:

"keyValuePairs.keySet()"= HashMap$1  (id=830062763448)  
[ LocationId,  Default,  Editable,  Name, Id,  Transcribed]

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) and group(2) matches; I'm sure you'll find that it matches the extra whitespaces.

A quick fix is to put group(1).trim() and group(2).trim() into the map instead, but the better option is to fix the regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文