简体   繁体   English

使用hashMap <STring,String> 作为我的HashMap中的关键对象 <K,V>

[英]using hashMap<STring,String> as a key object in my HashMap<K,V>

I asked a previous question on SO searching for specific keys in multiple independent hashMap objects about how to implemen this, or if I should implement a specific new class for my key. 我问了一个先前的问题,关于如何在多个独立的hashMap对象中搜索特定键,有关如何实现此键,或者是否应该为我的键实现特定的新类。

Eventually, after further reading of the docs, and noting that HashMap does already implement an equals() and hashcode() method I decided I would be good to go with using a HashMap as a key in my code. 最终,在进一步阅读文档之后,注意到HashMap已经实现了equals()和hashcode()方法,我决定在代码中使用HashMap作为键是不错的选择。

I've come to testing, and I've build an object of the following type HashMap< HashMap, Object> it is in fact built from info in a database. 我已经进行了测试,并且已经构建了以下类型的对象HashMap <HashMap,Object>,它实际上是根据数据库中的信息构建的。

Object itself contains a member of type HashMap 对象本身包含HashMap类型的成员

so I loop over my Object member, and print the result... 所以我遍历了我的Object成员,并打印结果...

for (HashMap<String, String> keVal: Object.getMember().keySet())
    {
        System.out.println("the key of: " + keVal.toString() +"has a hash code of " + keVal.hashCode() +
                "here is the data for this key:\n" +
                Object.getMember().get(keVal).getColData().toString() );

    }

You will notice that I have printed out the info for the keVal regarding contents and its hash code, for good measure, then on the following line I print out the info in the Member that relates to the specified keVal information. 您会注意到,出于良好的考虑,我已经打印出了有关内容及其哈希码的keVal信息,然后在下一行中,在成员中打印了与指定keVal信息相关的信息。 The problem is however that I always get a return of the same line of information from my map. 但是问题是,我总是从地图中得到同一行信息。

here is a small snippet of the output 这是输出的一小段

the key of: {OBS=002018}has a hash code of 1420118533 here is the data for this key:
{OBS=002035, CHIMIO1=null, CHIMIO2=null, DTHEMO=2011-11-09 00:00:00, PNN=7.4, ATCDND=0, HEMA=4.21, ALAT=23.0, LYMP=1.6, CHIMIOT1=0, CHIMIOT2=0, PNE=0.1, PNB=0.0, TAILLE=168, NBCY1=null, RXANT=0, NBCY2=null, CHIMIOX2=null, CONTRAC=1, CHIMIOX1=null, DTFCHIMIO1=null, DTFCHIMIO2=null, ASAT=21.0, ECRIT11=0, ECRIT10=0, SCORPO=1.81, MONO=0.5, HEMATO=38.2, CHIMIOANT=0, ID_VISIT=0, DTDCHIMIO2=null, DTDCHIMIO1=null, ECRIT25=0, ECRIT24=0, ECRIT23=0, LEUCO=9.6, ECRIT7=0, ECRIT8=0, ECRIT9=0, HB=13.2, ECRIT20=0, ECRIT21=0, ECRIT22=0, ECRIT1=0, ICRIT1=1, ECRIT2=0, ECRIT3=0, ECRIT4=0, ECRIT5=0, ECRIT6=0, PLQ=214.0, MENO=0, DTBIOCH=2011-11-09 00:00:00, ECRIT13=0, ECRIT12=0, ECRIT15=0, ECRIT14=0, ECRIT17=0, ECRIT16=0, ECRIT19=0, ECRIT18=0}
the key of: {OBS=002017}has a hash code of 1420118532 here is the data for this key:
{OBS=002035, CHIMIO1=null, CHIMIO2=null, DTHEMO=2011-11-09 00:00:00, PNN=7.4, ATCDND=0, HEMA=4.21, ALAT=23.0, LYMP=1.6, CHIMIOT1=0, CHIMIOT2=0, PNE=0.1, PNB=0.0, TAILLE=168, NBCY1=null, RXANT=0, NBCY2=null, CHIMIOX2=null, CONTRAC=1, CHIMIOX1=null, DTFCHIMIO1=null, DTFCHIMIO2=null, ASAT=21.0, ECRIT11=0, ECRIT10=0, SCORPO=1.81, MONO=0.5, HEMATO=38.2, CHIMIOANT=0, ID_VISIT=0, DTDCHIMIO2=null, DTDCHIMIO1=null, ECRIT25=0, ECRIT24=0, ECRIT23=0, LEUCO=9.6, ECRIT7=0, ECRIT8=0, ECRIT9=0, HB=13.2, ECRIT20=0, ECRIT21=0, ECRIT22=0, ECRIT1=0, ICRIT1=1, ECRIT2=0, ECRIT3=0, ECRIT4=0, ECRIT5=0, ECRIT6=0, PLQ=214.0, MENO=0, DTBIOCH=2011-11-09 00:00:00, ECRIT13=0, ECRIT12=0, ECRIT15=0, ECRIT14=0, ECRIT17=0, ECRIT16=0, ECRIT19=0, ECRIT18=0}
the key of: {OBS=002019}has a hash code of 1420118534 here is the data for this key:
{OBS=002035, CHIMIO1=null, CHIMIO2=null, DTHEMO=2011-11-09 00:00:00, PNN=7.4, ATCDND=0, HEMA=4.21, ALAT=23.0, LYMP=1.6, CHIMIOT1=0, CHIMIOT2=0, PNE=0.1, PNB=0.0, TAILLE=168, NBCY1=null, RXANT=0, NBCY2=null, CHIMIOX2=null, CONTRAC=1, CHIMIOX1=null, DTFCHIMIO1=null, DTFCHIMIO2=null, ASAT=21.0, ECRIT11=0, ECRIT10=0, SCORPO=1.81, MONO=0.5, HEMATO=38.2, CHIMIOANT=0, ID_VISIT=0, DTDCHIMIO2=null, DTDCHIMIO1=null, ECRIT25=0, ECRIT24=0, ECRIT23=0, LEUCO=9.6, ECRIT7=0, ECRIT8=0, ECRIT9=0, HB=13.2, ECRIT20=0, ECRIT21=0, ECRIT22=0, ECRIT1=0, ICRIT1=1, ECRIT2=0, ECRIT3=0, ECRIT4=0, ECRIT5=0, ECRIT6=0, PLQ=214.0, MENO=0, DTBIOCH=2011-11-09 00:00:00, ECRIT13=0, ECRIT12=0, ECRIT15=0, ECRIT14=0, ECRIT17=0, ECRIT16=0, ECRIT19=0, ECRIT18=0}
the key of: {OBS=002014}has a hash code of 1420118529 here is the data for this key:
{OBS=002035, CHIMIO1=null, CHIMIO2=null, DTHEMO=2011-11-09 00:00:00, PNN=7.4, ATCDND=0, HEMA=4.21, ALAT=23.0, LYMP=1.6, CHIMIOT1=0, CHIMIOT2=0, PNE=0.1, PNB=0.0, TAILLE=168, NBCY1=null, RXANT=0, NBCY2=null, CHIMIOX2=null, CONTRAC=1, CHIMIOX1=null, DTFCHIMIO1=null, DTFCHIMIO2=null, ASAT=21.0, ECRIT11=0, ECRIT10=0, SCORPO=1.81, MONO=0.5, HEMATO=38.2, CHIMIOANT=0, ID_VISIT=0, DTDCHIMIO2=null, DTDCHIMIO1=null, ECRIT25=0, ECRIT24=0, ECRIT23=0, LEUCO=9.6, ECRIT7=0, ECRIT8=0, ECRIT9=0, HB=13.2, ECRIT20=0, ECRIT21=0, ECRIT22=0, ECRIT1=0, ICRIT1=1, ECRIT2=0, ECRIT3=0, ECRIT4=0, ECRIT5=0, ECRIT6=0, PLQ=214.0, MENO=0, DTBIOCH=2011-11-09 00:00:00, ECRIT13=0, ECRIT12=0, ECRIT15=0, ECRIT14=0, ECRIT17=0, ECRIT16=0, ECRIT19=0, ECRIT18=0}
the key of: {OBS=002013}has a hash code of 1420118528 here is the data for this key:
{OBS=002035, CHIMIO1=null, CHIMIO2=null, DTHEMO=2011-11-09 00:00:00, PNN=7.4, ATCDND=0, HEMA=4.21, ALAT=23.0, LYMP=1.6, CHIMIOT1=0, CHIMIOT2=0, PNE=0.1, PNB=0.0, TAILLE=168, NBCY1=null, RXANT=0, NBCY2=null, CHIMIOX2=null, CONTRAC=1, CHIMIOX1=null, DTFCHIMIO1=null, DTFCHIMIO2=null, ASAT=21.0, ECRIT11=0, ECRIT10=0, SCORPO=1.81, MONO=0.5, HEMATO=38.2, CHIMIOANT=0, ID_VISIT=0, DTDCHIMIO2=null, DTDCHIMIO1=null, ECRIT25=0, ECRIT24=0, ECRIT23=0, LEUCO=9.6, ECRIT7=0, ECRIT8=0, ECRIT9=0, HB=13.2, ECRIT20=0, ECRIT21=0, ECRIT22=0, ECRIT1=0, ICRIT1=1, ECRIT2=0, ECRIT3=0, ECRIT4=0, ECRIT5=0, ECRIT6=0, PLQ=214.0, MENO=0, DTBIOCH=2011-11-09 00:00:00, ECRIT13=0, ECRIT12=0, ECRIT15=0, ECRIT14=0, ECRIT17=0, ECRIT16=0, ECRIT19=0, ECRIT18=0}

As you can see the value retrieved is always related to OBS=002035, which interestingly is the last value in the map (if I iterate over it by retrieving the array of members) but not in the above printout? 如您所见,检索到的值始终与OBS = 002035有关,有趣的是,它是映射中的最后一个值(如果我通过检索成员数组对其进行迭代),但不在上面的打印输出中? Point of note, no matter how many times I run the code I always get a copy of OBS=002035 as my value returned for the specified key! 注意事项,无论我运行了多少次代码,我总是得到OBS = 002035的副本,作为我为指定键返回的值!

I'm a bit lost, is it a bug in HashMap^? 我有点迷茫,这是HashMap中的错误^吗? Or an issue in how I'm retriveing the keVal member. 还是我如何检索keVal成员的问题。 Or is it that the first line of the for loop is in fact a potential classCastException, that I need to deal with somehow? 还是for循环的第一行实际上是潜在的classCastException,我需要以某种方式处理?

If I implement my own personal keyObject, that then has a HashMap member, and implment equals() and hashCode() with respect to the values in the HashMap member will I still have the same problem? 如果我实现自己的个人keyObject,则该对象具有HashMap成员,并且相对于HashMap成员中的值实现equals()和hashCode(),我还会遇到同样的问题吗? (that may be a bit of a fesicious question as I'm about to do this any how and test the result). (这可能是一个小问题,因为我将以任何方式进行此操作并测试结果)。

edit1: I may have rogered my code elsewhere, see my response to Louis below. edit1:我可能在其他地方也弄糟了我的代码,请参阅下面对Louis的回复。 Once I confirm this is where the problem is I ask for the mods to delete the question as a 'dopey programmer' problem not a real problem ! 一旦确认这是问题所在,我就要求mods将该问题删除为“傻瓜程序员”问题,而不是真正的问题!

OK I found the problem... 好,我发现了问题...

Whilst reading the code that I was going to post, and adding in some extra comments I found the problem, and solution. 在阅读我要发布的代码并添加一些额外的注释时,我发现了问题和解决方案。

the flow of the program was as follows: 该程序的流程如下:

Grab data with a "select * from [tableName]" loop over the result set, putting each row into a temp object. 使用“从[tableName]中选择*”获取数据,在结果集上循环,将每一行放入一个临时对象。

Problem / solution 问题方案

I created my temp object to hold the data before I issued my while(resultSet.next()) loop. 在发出while(resultSet.next())循环之前,我创建了临时对象来保存数据。 Inside this loop I added info into the above temp object. 在此循环中,我将信息添加到上述临时对象中。

Before moving to the next row in the resultSet (ie the next while iteration) I passed the temp object somewhere else. 在移到resultSet的下一行之前(即下一次迭代时),我将temp对象传递到其他地方。

However although during the debut the report of the info in the temp object was correct, the process of using it seemed to use the original version created not the one related to the current iteration of the while loop? 但是,尽管在首次亮相期间temp对象中的信息报告是正确的,但是使用它的过程似乎使用了创建的原始版本,而不是与while循环的当前迭代相关的版本?

Moving the creation of my temp object into the inside of my while loop solved the problem. 将我的临时对象的创建移到while循环内部解决了该问题。 Here it is in pseudo code... 这是伪代码...

//bad code
ResultSet rs =  getDataFromDBMS("Select * from [tableName];");

Object temp = new objectToHoldInfoFromResultSet();

//loop over the result set
while (rs.next)//for each row in the result set
        {
        for(int i=1; i<=rs.getNumberColums; i++)
                {
                temp.add(infoAboutColumn);
                }
        temp.printInfo();//prints correct info
        anotherObject(makeUseOf(temp));//use info from first iteration.


        }

//Seemingly each loop into the while the temp.doSomethingToData(); uses the temp object created in the first iteration

//good code
ResultSet rs =  getDataFromDBMS("Select * from [tableName];");


//loop over the result set
while (rs.next)//for each row in the result set
        {
         Object temp = new objectToHoldInfoFromResultSet();//moving declaration of temp into the while loop solves the problem.
        for(int i=1; i<=rs.getNumberColums; i++)
                {
                temp.add(infoAboutColumn);
                }
        temp.printInfo();//prints current info

        anotherObject(makeUseOf(temp));//uses the current info.


        }

so while the above has solved the problem it has given rise to another question. 因此,尽管上述解决了问题,但又引发了另一个问题。

the temp.printInfo(); temp.printInfo(); call always prints the correct 'current' information obtained in the while loop. 调用总是打印在while循环中获得的正确“当前”信息。 the anotherObject(makeUseOf(temp) however calls the temp object from outside of the while loop (if that is where I have created it) - ie it doesn't seem to reference the same temp object as the temp.printInfo() call does. anotherObject(makeUseOf(temp)但是从while循环外部调用temp对象(如果这是我在其中创建的地方)-即它似乎没有像temp.printInfo()那样引用相同的临时对象。 。

I'll Ask this in another question, and put in a link... here is the new question 我将在另一个问题中提出这个问题,并放入一个链接... 这是新问题

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

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