HashMap (Java) 给出错误的结果
我创建了一个哈希映射,其中每个条目对应 3 个值 关键对象值(数量为两个)
我创建了一个类,我创建了其对象并将结果存储在哈希映射中 这是我下面的代码,其中我将传入的数据与哈希映射中的先前值进行比较。如果出现相同的数据,那么我只需增加该数据的计数器。我已经在 for 循环中获取了 print 语句。尽管两个字符串匹配,但我的代码仍然从未进入 if 循环以增加计数器。为什么?
for(i=1;i<=hMap.size();i++)
{
String skey = Integer.toString(i);
if(hMap.get(skey).olddata==comingdata)
{
hMap.get(skey).counter= hMap.get(skey).counter+1;
}
}
I have created a hash map in which each entry corresponds to 3 values
Key object values ( which are two in number)
I have created a class ,whose object i create and store the results in a hash map
This is my code below in which i compare my incoming data with the previous values in the hash map.If the same data comes then i just increment the counter of that data. I have taken the print statements in the the for loop . though the two strings match but still my code never comes in the if loop for increment the counter.Why?
for(i=1;i<=hMap.size();i++)
{
String skey = Integer.toString(i);
if(hMap.get(skey).olddata==comingdata)
{
hMap.get(skey).counter= hMap.get(skey).counter+1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还没有提供有关所涉及类型的足够信息,但是,但我强烈怀疑这就是问题所在:
如果
olddata引用,而不是相等性code> 和
comingdata
是某种类型的引用。 (编辑:从它的声音来看,它们是字符串引用。)我的猜测是你想要:
或者更有效地避免无意义的查找:
我还建议如果你总是要按整数查找,为什么不使用
Integer
键而不是总是将整数转换为字符串?此外,遵循 Java 命名约定是值得的,并且您应该将字段设为私有(如果尚未设为私有)。
如果这些都没有帮助,请发布更多代码。问题出现在
HashMap
中而不是出现在代码中的可能性非常小。You haven't given nearly enough information about the types involved, but, but I strongly suspect that this is the problem:
That will be comparing references, rather than for equality, if
olddata
andcomingdata
are references of some kind. (EDIT: By the sounds of it, they're string references.)My guess is that you want:
Or rather more efficiently, avoiding pointless lookups:
I'd also suggest that if you're always going to look up by an integer, why not use an
Integer
key instead of always converting the integer into a string?Additionally, it's worth following Java naming conventions, and you should make your fields private if they're not already.
If none of this helps, please post more code. The chances of the problem being in
HashMap
rather than in your code are incredibly small.目前尚不清楚
olddata
的类型,但也许您应该使用equals()
来比较值:在 Java 中,
==
用于比较原始数据类型的相等性或比较对象类型的同一性。如果您需要比较两个对象类型的相等性,则必须使用 equals() 方法,该方法是为所有对象定义的,因为它继承自 Object 类,请注意您还必须重写类中的equals()
和hashCode()
,提供对该类有意义的实现。It's not clear the type of
olddata
, but maybe you should compare the values usingequals()
:In Java,
==
is used for either comparing primitive data types for equality or comparing object types for identity. If you need to compare two object types for equality, then you must use theequals()
method, which is defined for all objects since it's inherited from theObject
class, being aware that you also must overrideequals()
andhashCode()
in your class, providing implementations meaningful for that class.在 Java 中,您不会将对象与
==
进行比较,除非您试图查看它们是否具有相同的参考值。您也不应该像那样公开
olddata
;它应该可以通过 getter 获得;例如getOldData()
You don't compare objects with
==
in Java unless you're trying to see if they have the same reference value.You also shouldn't be exposing
olddata
like that; it should be available via a getter; e.g.getOldData()
应该是
It should be
您实际上是指
comingdata.equals(hMap.get(skey).olddata)
吗?此外,请注意equals(Object)
和hashCode()
必须正确实现。Do you actually mean
comingdata.equals(hMap.get(skey).olddata)
? Furthermore be aware thatequals(Object)
andhashCode()
must be correclty implemented.