首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

一个hash地图相关的有关问题

2012-07-31 
求助一个hashmap相关的问题有一个程序,需要读取数据数据结构如下,这种数据几万条,我要把这些数据录入到数

求助一个hashmap相关的问题
有一个程序,需要读取数据
数据结构如下,这种数据几万条,我要把这些数据录入到数据库中,我想写一个程序,用hashmap来存储等号两端的数据

SQL code
*NEWRECORDRECTYPE = DMH = Tobacco Use Cessation ProductsAQ = AE CL CT EC ES HI MI PS SD SN ST TD UT VE VIENTRY = Commit|T203|TRD|NRW|NLM (2002)|021106|abcdefENTRY = Nicorette|T203|TRD|NRW|NLM (1987)|abcdeENTRY = Nicotine Chewing Gum|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Inhalant|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Lozenge|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Lozenges|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Nasal Spray|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Patch|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Polacrilex|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Replacement Products|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Nicotine Transdermal Patch|T203|NON|NRW|NLM (2012)|110705|abcdefENTRY = Smoking Cessation Products|T074|NON|REL|NLM (2012)|110705|abcdefENTRY = Cessation Product, SmokingENTRY = Cessation Products, SmokingENTRY = Chewing Gum, NicotineENTRY = Chewing Gums, NicotineENTRY = Gum, Nicotine ChewingENTRY = Gums, Nicotine ChewingENTRY = Inhalant, NicotineENTRY = Inhalants, NicotineENTRY = Lozenge, NicotineENTRY = Lozenges, NicotineENTRY = Nasal Spray, NicotineENTRY = Nasal Sprays, NicotineENTRY = Nicotine Chewing GumsENTRY = Nicotine InhalantsENTRY = Nicotine Nasal SpraysENTRY = Nicotine PolacrilicesENTRY = Nicotine Replacement ProductENTRY = Patch, NicotineENTRY = Patch, Nicotine TransdermalENTRY = Polacrilex, NicotineENTRY = Polacrilices, NicotineENTRY = Product, Nicotine ReplacementENTRY = Product, Smoking CessationENTRY = Products, Nicotine ReplacementENTRY = Products, Smoking CessationENTRY = Replacement Product, NicotineENTRY = Replacement Products, NicotineENTRY = Smoking Cessation ProductENTRY = Spray, Nicotine NasalENTRY = Sprays, Nicotine NasalENTRY = Transdermal Patch, NicotineMN = J01.637.847FX = Tobacco Use CessationMH_TH = NLM (2012)ST = T074MS = Items used to aid in ending a TOBACCO habit.PM = 2012; NICOTINE POLACRILEX was indexed under NICOTINE, POLYVINYLS, and POLYMETHACRYLIC ACIDS 1987-2011HN = 2012DA = 20110705DC = 1DX = 20120101UI = D061485

每次在存储完一条数据后,我都会重新实例化一个hashmap,结果每次我在新的hashmap里面调用add方法之后,上一个实例中的数据又会添加到本次实例化的hashmap中,请问这个如何解决

[解决办法]
else if(hashmap.size() != 0) {
arr.add(hashmap);
hashmap = new HashMap();
}
不对,
应该把
if(hashmap.size() != 0) {
arr.add(hashmap);
hashmap = new HashMap();
}
放在
default:
break;
}
// arrstr.add(hashmap);
后面,也就是if{}的里面吧。。。
[解决办法]
你所谓的存储一条数据不是指的是一行么???
[解决办法]
1、这句话没看懂是什么意思?hashmap并没有add()函数可以让你调用。
“结果每次我在新的hashmap里面调用add方法之后,上一个实例中的数据又会添加到本次实例化的hashmap中”


2、如果你是说新行(*NEWRECORD)的数据仍然进入了之前hashmap的话,从代码逻辑上来看并没有发现存在这种问题,你是怎么检查发现有这种问题的?


3、发现你的cttarr,是跨所有hashmap共用一个的,是否符合原始设计意图?
[解决办法]
Map<?> map = new HashMap<ArrayList<String>>();
List<String> list = map.get(key);
if(null == list)
{
list = new ArrayList<String>();
list.add(data);
map.put(key,list);
}else{
list.add(data);
}

先根据key取出Map中的集合,如果取出为null,也就是不存在这个键值对,创建List再put进去。
如果不为null,直接取出add新值就OK,这里集合都是引用传递。

热点排行