日期:2014-05-19  浏览次数:20796 次

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

*NEWRECORD
RECTYPE = D
MH = Tobacco Use Cessation Products
AQ = AE CL CT EC ES HI MI PS SD SN ST TD UT VE VI
ENTRY = Commit|T203|TRD|NRW|NLM (2002)|021106|abcdef
ENTRY = Nicorette|T203|TRD|NRW|NLM (1987)|abcde
ENTRY = Nicotine Chewing Gum|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Inhalant|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Lozenge|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Lozenges|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Nasal Spray|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Patch|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Polacrilex|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Replacement Products|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Nicotine Transdermal Patch|T203|NON|NRW|NLM (2012)|110705|abcdef
ENTRY = Smoking Cessation Products|T074|NON|REL|NLM (2012)|110705|abcdef
ENTRY = Cessation Product, Smoking
ENTRY = Cessation Products, Smoking
ENTRY = Chewing Gum, Nicotine
ENTRY = Chewing Gums, Nicotine
ENTRY = Gum, Nicotine Chewing
ENTRY = Gums, Nicotine Chewing
ENTRY = Inhalant, Nicotine
ENTRY = Inhalants, Nicotine
ENTRY = Lozenge, Nicotine
ENTRY = Lozenges, Nicotine
ENTRY = Nasal Spray, Nicotine
ENTRY = Nasal Sprays, Nicotine
ENTRY = Nicotine Chewing Gums
ENTRY = Nicotine Inhalants
ENTRY = Nicotine Nasal Sprays
ENTRY = Nicotine Polacrilices
ENTRY = Nicotine Replacement Product
ENTRY = Patch, Nicotine
ENTRY = Patch, Nicotine Transdermal
ENTRY = Polacrilex, Nicotine
ENTRY = Polacrilices, Nicotine
ENTRY = Product, Nicotine Replacement
ENTRY = Product, Smoking Cessation
ENTRY = Products, Nicotine Replacement
ENTRY = Products, Smoking Cessation
ENTRY = Replacement Product, Nicotine
ENTRY = Replacement Products, Nicotine
ENTRY = Smoking Cessation Product
ENTRY = Spray, Nicotine Nasal
ENTRY = Sprays, Nicotine Nasal
ENTRY = Transdermal Patch, Nicotine
MN = J01.637.847
FX = Tobacco Use Cessation
MH_TH = NLM (2012)
ST = T074
MS = Items used to aid in ending a TOBACCO habit.
PM = 2012; NICOTINE POLACRILEX was indexed under NICOTINE, POLYVINYLS, and POLYMETHACRYLIC ACIDS 1987-2011
HN = 2012
DA = 20110705
DC = 1
DX = 20120101
UI = 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,这里集合都是引用传递。