Java hashmap找不到现有条目 [英] Java hashmap cannot find existing entry

查看:187
本文介绍了Java hashmap找不到现有条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个非常奇怪的问题:我编写了一个CSV解析器,它将第一行作为标题读取,然后创建一个映射,其中key是标题,value是后续行中的值。



问题是地图似乎无法检索第一列标题的值。如此处所示:



如您所见, abc位于HashMap条目#23中,但是当我检索它时,它将显示为null。



我认为这与我的CSV文件有关,但是为什么映射项正确显示在调试器但无法检索?



我的CSV文件如下所示:


abc数据
源,实体,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24
NA,source1,entity1,101,102,103,104,105,106,107,108,109,110,110,111,112,113,114,115,116,117,118,119,120,121,122,122,123,124



您观察到的调试器结果的唯一合理解释是,第一列标题实际上是 not abc 。其中可能包含一个或多个其他非打印字符。当然,这些不会打印在调试器的地图内容列表中,但是它们仍然存在,因此,键 abc 确实不在地图中。实际上,它类似于< some_non-printing_character> abc 。或者,可能是您拥有不同的字符,这些字符在显示器上呈现为相同。无论哪种方式,都需要使用十六进制编辑器检查输入文件以了解真相。



该列为第一列,一个可能的额外字符候选字是Unicode BOM ,U + FEFF,显示为文件的第一个字符。一些文本编辑器会插入诸如魔术数字之类的字符,以将文件标记为以UTF-8进行编码,尽管实际上UTF-8并不需要这样做,除了赋予其本身以外,其他任何字符都没有意义。


I am running into a very weird problem: I wrote a CSV parser where it reads the first row as headers, then creates a map where key is the header and value is the values in subsequent rows.

The issue is the map cannot seem to retrieve the value of the header of the first column. As seen here:

as you can see, "abc" is inside the HashMap entry #23, but when I am retrieving it, it would show as null.

I think it has something to do with my CSV file but why is the map entries showing up correctly on debugger but cannot retrieve?

my CSV file looks like this:

abc,Data Source,Entity,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 NA,source1,entity1,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124

解决方案

The only plausible explanation for the debugger results you observe is that the first column header is not actually "abc". It likely has one or more additional, non-printing characters within. These do not print, naturally, in the debugger's list of the map's contents, but they are nevertheless there, so indeed the key "abc" is not present in the map. It is actually something like "<some_non-printing_character>abc". Alternatively, it may be that you have different characters that are rendered the same on your display. Either way, examine the input file with a hex editor to see the truth.

The column in question being the first, a likely candidate for an extra character is a Unicode BOM, U+FEFF, appearing as the first character of the file. Some text editors will insert such a character as a magic number marking files as being encoded in UTF-8, though in fact UTF-8 does not require that, and assigns no significance to such a character other than as itself.

这篇关于Java hashmap找不到现有条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆