日期:2014-05-20  浏览次数:20677 次

怎么将一个vector装换成一个hashmap
Vector abc = new Vector();
abc.add("begindate=2012-05-16, makerdate=2012-05-16, expiredate=2012-06-16, saleschannelid=12, programcode=SS100001, maker=10000289, description=特价支架测试, datelastupdated=2012-05-16, ssbiid=100000533");

abc.add("begindate=2012-05-16, makerdate=2012-05-16, expiredate=2012-06-16, saleschannelid=12, programcode=SS100002, maker=10000289, description=支架特价测试, datelastupdated=2012-05-16, ssbiid=100000536");

for(int i=0;i<abc.size();i++){

  HashMap hm=(HashMap)abc.get(i); //这个地方就出错了呢?
}
}

------解决方案--------------------
abc.get(i)是一个字符串,怎么转能HashMap?


Java code

HashMap hm = new HashMap();
for(int i=0;i<abc.size();i++){
     hm.put(i, abc.get(i));
}

------解决方案--------------------
字符串就是字符串,强制转化为其他类型都会报错
取出来后按照逗号分割成每个键值对,然后按照等号分割成key和value,然后将这些存储到Map里面
Java code

    Vector<String>  abc = new Vector<String>();
        abc.add("begindate=2012-05-16, makerdate=2012-05-16, expiredate=2012-06-16, saleschannelid=12, programcode=SS100001, maker=10000289, description=特价支架测试, datelastupdated=2012-05-16, ssbiid=100000533");

        abc.add("begindate=2012-05-16, makerdate=2012-05-16, expiredate=2012-06-16, saleschannelid=12, programcode=SS100002, maker=10000289, description=支架特价测试, datelastupdated=2012-05-16, ssbiid=100000536");
        Vector<Map<String,String>>  efg = new Vector<Map<String,String>>();
        for(int i=0;i<abc.size();i++)
        {
          String[] kvs = abc.get(i).split(",");
          HashMap<String, String> hm=new HashMap<String, String>(); 
          for(String kv:kvs)
          {
              kv = kv.trim();
              String key = kv.split("=")[0];
              String value = kv.split("=")[1];
              hm.put(key, value);
          }
          efg.add(hm);
        }