简体   繁体   English

HashMap(Java)给出错误的结果

[英]HashMap (Java) giving wrong results

I have created a hash map in which each entry corresponds to 3 values Key object values ( which are two in number) 我创建了一个哈希映射,其中每个条目对应于3个值键对象值(数量为两个)

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 . 我已经在for循环中使用了print语句。 though the two strings match but still my code never comes in the if loop for increment the counter.Why? 虽然两个字符串匹配,但是我的代码仍然永远不会进入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;
      }
 }

You haven't given nearly enough information about the types involved, but, but I strongly suspect that this is the problem: 您尚未提供有关所涉及类型的足够多的信息,但是,我强烈怀疑这是问题所在:

if(hMap.get(skey).olddata==comingdata)

That will be comparing references , rather than for equality, if olddata and comingdata are references of some kind. 如果olddatacomingdata是某种类型的引用 ,那将是比较引用 ,而不是相等性。 (EDIT: By the sounds of it, they're string references.) (编辑:听起来,它们是字符串引用。)

My guess is that you want: 我的猜测是您想要:

String skey = Integer.toString(i);
if(hMap.get(skey).olddata.equals(comingdata))
{
   hMap.get(skey).counter=  hMap.get(skey).counter+1;
}

Or rather more efficiently, avoiding pointless lookups: 或更有效地避免无意义的查找:

WhateverType value = hMap.get(Integer.toString(i));
if (value.olddata.equals(comingdata))
{
    value.counter++;
}

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? 我还建议,如果您始终要查找整数,为什么不使用Integer键而不是始终将整数转换为字符串?

Additionally, it's worth following Java naming conventions, and you should make your fields private if they're not already. 另外,值得遵循Java命名约定,如果尚未设置字段,则应将其设为私有。

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. 问题出现在HashMap而不是代码中的可能性非常小。

You don't compare objects with == in Java unless you're trying to see if they have the same reference value . 除非试图查看对象是否具有相同的参考值,否则不要在Java中将它们与==进行比较。

if (hMap.get(skey).olddata.equals(comingdata)) {
...

You also shouldn't be exposing olddata like that; 您也不应该像这样暴露olddata it should be available via a getter; 它应该可以通过吸气剂获得; eg getOldData() 例如getOldData()

It's not clear the type of olddata , but maybe you should compare the values using equals() : 目前尚不清楚olddata的类型,但也许您应该使用equals()比较这些值:

if (hMap.get(skey).olddata.equals(comingdata))

In Java, == is used for either comparing primitive data types for equality or comparing object types for identity . 在Java中, ==用于比较原始数据类型是否相等或比较对象类型是否为identity If you need to compare two object types for equality , then you must use the equals() method, which is defined for all objects since it's inherited from the Object class, being aware that you also must override equals() and hashCode() in your class, providing implementations meaningful for that class. 如果需要比较两种对象类型的相等性 ,则必须使用equals()方法,该方法为所有对象定义,因为它是从Object类继承的,因此请注意,您还必须重写equals()hashCode() 。您的课程,提供对该课程有意义的实现。

它应该是

if(hMap.get(skey).olddata.equals(comingdata))

Do you actually mean comingdata.equals(hMap.get(skey).olddata) ? 您实际上是指comingdata.equals(hMap.get(skey).olddata)吗? Furthermore be aware that equals(Object) and hashCode() must be correclty implemented. 此外,请注意必须正确实现equals(Object)hashCode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM