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

故事扮演 提交于 2019-12-13 03:36:49

问题


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.

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.

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.

Object itself contains a member of type HashMap

so I loop over my Object member, and print the result...

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. 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? 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!

I'm a bit lost, is it a bug in HashMap^? Or an issue in how I'm retriveing the keVal member. Or is it that the first line of the for loop is in fact a potential classCastException, that I need to deal with somehow?

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? (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. 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 !


回答1:


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.

Problem / solution

I created my temp object to hold the data before I issued my while(resultSet.next()) loop. 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.

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?

Moving the creation of my temp object into the inside of my while loop solved the problem. 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(); call always prints the correct 'current' information obtained in the while loop. 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.

I'll Ask this in another question, and put in a link...here is the new question



来源:https://stackoverflow.com/questions/11241215/using-hashmapstring-string-as-a-key-object-in-my-hashmapk-v

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!