HashMap的值被覆盖 [英] Values of HashMap are overriden
本文介绍了HashMap的值被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个HashMap。在创建它时,我可以看到键被分配了适当的不同值,但是,在创建之后,当我迭代它时,所有键只产生一个值。以下是我的代码:
I''m creating a HashMap. While creating it, I can see that keys are assigned proper distinct values, but, after creation, when I iterate it, all the keys are yielding only one value. Below is my code:
public void prepareAccountMap(HashMap<String, Object> masterMap, GAReadConfigType config, Properties prop){
int count=0;
for(Map.Entry<String, Object> entry : masterMap.entrySet()){
String key = entry.getKey();
keyList.add(count, key);
count++;
String childFile = (String)entry.getValue();
System.out.println("First loop, childFile :"+childFile);
if(childFile!=null){
/*
* 1) prop.getProperty gets the path from properties file
* 2) config.readConfig reads the xml file and prepares a HashMap
*/
HashMap<String, Object> tempChildMap = config.readConfig(prop.getProperty(GAConstants.GA_XML_CHILD)+ childFile);
accountMap.put(key, tempChildMap.get(GAConstants.GA_DUMMY_KEY));
GAAccountBean beanTest = (GAAccountBean)tempChildMap.get(GAConstants.GA_DUMMY_KEY);
System.out.println("First loop, key :"+key);
System.out.println("First loop, bean dimension :"+beanTest.getDimensionsFromXML().trim());
}
}
System.out.println("");
for(Map.Entry<String, Object> entry : accountMap.entrySet()){
String key = entry.getKey();
GAAccountBean tempBean = (GAAccountBean)accountMap.get(key);
System.out.println("Second Loop, key :"+key);
System.out.println("Second Loop, tempBean dimension :"+tempBean.getDimensionsFromXML().trim());
}
}
以下是控制台输出:
Below is the console ouput:
First loop, childFile :\Acct_38058226\Prop_66591454\Prof_68497513\NJTReport2.xml
First loop, key :NJTReport2.xml
First loop, bean dimension :ga:visitorType,ga:date
First loop, childFile :\Acct_38058226\Prop_66591454\Prof_68497513\TestReportNJT1.xml
First loop, key :TestReportNJT1.xml
First loop, bean dimension :ga:browser,ga:date
Second Loop, key :NJTReport2.xml
Second Loop, tempBean dimension :ga:browser,ga:date
Second Loop, key :TestReportNJT1.xml
Second Loop, tempBean dimension :ga:browser,ga:date
由于存储在HashMap中的值是一个bean,我正在打印bean的一个属性。希望我对我的问题很清楚。如果没有,请让我知道警告,我会填写它。谢谢!
Since the value stored in HashMap is a bean, I''m printing one property of the bean. Hope I''m clear with my question. If not, please let me know the caveat and I shall fill it.Thanks in advance!!
推荐答案
我已经解决了!!必须为每个bean创建一个新对象.....
I''ve solved it!! Had to create a new object for every bean.....
这篇关于HashMap的值被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文