java为什么在使用简单json库后无法比较字符串?
代码:
HashMap<String, String> map = new HashMap<>();
for(Object type:entityObj.keySet()){
String notableType=String.valueOf(type);
System.out.println(notableType);
String object="NotableType";
if(notableType.equals(object)){
map.put(entity.toString(), entityObj.get(type).toString());
System.out.println(map);
}
}
这很奇怪。我测试了String notableType
正是字符串“NotableType”,但是object
和notableType
之间的哈希值完全不同。一个是-1929035367,一个是0
为什么??如何解决这个问题
# 1 楼答案
他们当然不平等。您一定错过了字符串后面的空格。这些东西很容易错过
trim
填充字符串时应注意这一点