今天在写一个demo,从文件读取数据并计算单词在里面出现的次数,然后发现了一个让人不解的问题。
代码如下
File f = new File("D:\\sad2.txt");
try {
FileInputStream in = new FileInputStream(f);
byte[] b=new byte[1024];
int len=-1;
StringBuilder sb=new StringBuilder();
while ((len=in.read(b))!=-1){
sb.append(new String(b,0,len));
}
String[] split = sb.toString().split("\n");
Map<String,Integer> map=new HashMap<>();
for (String s : split) {
s = s.trim();
String[] s1 = s.split(" ");
Integer integer = map.get(s1[0]);
if (integer==null){
map.put(s1[0],Integer.parseInt(s1[1]));
}else {
map.put(s1[0],integer+Integer.parseInt(s1[1]));
}
}
map.forEach((k,v)->{
System.out.println(k+":"+v);
});
} catch (Exception e) {
e.printStackTrace();
}
文件里面的数据长这样
然后就出现了匪夷所思的事情
竟然会出现两个一模一样的键
然后发现字符串虽然一模一样,但是字节不相同,hash值也不同
但是我还是无法理解~
有知道的大佬欢迎指教