使用hashMap< STring,String>作为我的HashMap中的关键对象< K,V> [英] using hashMap<STring,String> as a key object in my HashMap<K,V>

查看:109
本文介绍了使用hashMap< STring,String>作为我的HashMap中的关键对象< K,V>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我询问了一个关于如何搜索特定键的问题在多个独立的哈希映射对象中有关如何实现这一点,或者我应该为我的密钥实现一个特定的新类。

最后,在进一步阅读docs,并注意到HashMap已经实现了equals()和hashcode()方法,所以我决定在我的代码中使用HashMap作为关键字。



我来测试,并且构建了以下类型的对象
HashMap< HashMap,Object>
它实际上是根据数据库中的信息构建的。



对象本身包含HashMap类型的成员



所以我循环遍历我的Object成员,并打印出结果...

pre $ for(HashMap< String,String> keVal:Object.getMember()。keySet())
{
System.out.println(key:+ keVal.toString()+的哈希码为+ keVal.hashCode()+
这里是这个键的数据:\\\
+
Object.getMember()。get(keVal).getColData()。toString());


$ / code>

您会注意到我已经打印出keVal关于内容和哈希代码,为了更好的衡量,然后在下面一行中,我输出成员中与指定keVal信息相关的信息。
然而,问题是我总是从我的地图中得到相同的信息。



这里是输出的一小段代码

 密钥:{OBS = 002018}的哈希码为1420118533这里是此密钥的数据:
{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 =空,DTDCHIMIO1 =空,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,ECRIT1 8 = 0}
{OBS = 002017}的密钥的哈希码为1420118532,这里是该密钥的数据:
{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 =空,DTDCHIMIO1 =空,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}
密钥:{OBS = 002019}的哈希码为1420118534,这里是该密钥的数据:
{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 = 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,ECRIT2 = 0,ECRIT8 = 0,ECRIT9 = 0,HB = 13.2,ECRIT20 = 0,ECRIT21 = 0,DTDCHIMIO2 =空,DTDCHIMIO1 =空,ECRIT25 = 0,ECRIT24 = 0,ECRIT23 = 0,LEUCO = 9.6, 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}
{OBS = 002014}的关键字有:哈希码1420118529这里是这个键的数据:
{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 =空,RXANT = 0,NBCY2 =空,CHIMIOX2 =空,CONTRAC = 1,CHIMIOX1 = null ,DTFCHIMIO1 =空,DTFCHIMIO2 =空,ASAT = 21.0,ECRIT11 = 0,ECRIT10 = 0,SCORPO = 1.81,MONO = 0.5,HEMATO = 38.2,CHIMIOANT = 0,ID_VISIT = 0,DTDCHIMIO2 = = 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}
{OBS = 002013}的密钥的哈希码为1420118528这里是此密钥的数据:
{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 =空,ASAT = 21.0,ECRIT11 = 0,ECRIT10 = 0,SCORPO = 1.81,MONO = 0.5,HEMATO = 38.2,CHIMIOANT = 0,ID_VISIT = 0,DTDCHIMIO2 =空,DTDCHIMIO1 =空,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}

正如你所看到的,检索到的值总是与OBS = 002035有关,这有趣的是映射中的最后一个值(如果我通过检索成员数组遍历它),但不是在上面的打印输出中?
值得注意的是,无论我运行多少次代码,我都会得到OBS = 002035的副本,因为我的值为指定的键返回!



我有点失落,这是HashMap中的错误吗?或者我是如何回顾keVal会员的问题。或者是for循环的第一行实际上是一个潜在的classCastException,我需要以某种方式处理?



如果我实现我自己的个人keyObject,然后有一个HashMap成员,并且对于HashMap成员中的值,implment equals()和hashCode()是否仍然有相同的问题? (这可能是一个有趣的问题,因为我正在做任何如何测试结果)。

edit1:
我可能有我的代码在其他地方摇摆,看到我对路易斯的回应如下。一旦我确认这是问题所在,我要求mods将这个问题作为dopey程序员问题删除,而不是真正的问题!

解决方案我发现问题...



在阅读我要发布的代码并添加一些额外评论时,我发现问题,和解决方案。

该程序的流程如下:

使用select * from [tableName]
遍历结果集,将每行放入临时对象中。



问题/解决方案



创建我的临时对象来保存数据,然后发出我的while(resultSet.next())循环。
在这个循环中,我将信息添加到上面的临时对象中。

在移动到resultSet的下一行之前(即迭代的下一行),我通过了然而,尽管在首次亮相时,临时对象中的信息报告是正确的,但是使用它的过程似乎使用了原来的版本而不是创建的与while循环的当前迭代有关吗?



将我的临时对象的创建移到我的while循环的内部解决了这个问题。这里是伪代码...



pre $ //错误代码
ResultSet rs = getDataFromDBMS(Select * from [表名];);

Object temp = new objectToHoldInfoFromResultSet();

//遍历结果集
while(rs.next)//结果集中的每一行
{
for(int i = 1; i< = rs.getNumberColums; i ++)
{
temp.add(infoAboutColumn);
}
temp.printInfo(); //输出正确的信息
anotherObject(makeUseOf(temp)); //使用第一次迭代的信息。


}

//看起来每个循环都进入temp.doSomethingToData();使用在第一次迭代中创建的临时对象

//良好的代码
ResultSet rs = getDataFromDBMS(Select * from [tableName];);


//遍历结果集
while(rs.next)//结果集中的每一行
{
Object temp = new objectToHoldInfoFromResultSet(); //将temp的声明移到while循环中解决问题。
for(int i = 1; i <= rs.getNumberColums; i ++)
{
temp.add(infoAboutColumn);
}
temp.printInfo(); //输出当前信息

anotherObject(makeUseOf(temp)); //使用当前信息。


}

所以虽然以上解决了问题它引起了另一个问题。

temp.printInfo();调用始终打印在while循环中获得的正确当前信息。
anotherObject(makeUseOf(temp)然而从while循环之外调用临时对象(如果这是我创建它的地方) - 也就是说它似乎没有引用与temp.printInfo相同的临时对象()调用。



我会在另一个问题中提出这个问题,然后放入链接... 这里是新的问题


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 !

解决方案

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

这篇关于使用hashMap&lt; STring,String&gt;作为我的HashMap中的关键对象&lt; K,V&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆