List、Set及Map的读取数据---Iterator遍历

来源:互联网 发布:新东方网络视频课怎样 编辑:程序博客网 时间:2024/05/16 19:21

¨ 

    List和Set都是Collection的子接口,区别如下:

Set是一个不包含重复元素的集合,集合内数据没有顺序

List通常被称作序列,允许重复元素,内部元素有特定的顺序

 

一开始,当然不能忘了最初的操作集合的方法

List<Integer> list=new List<Integer>();
for (int i=0; i<50000; i++) {
list.add(
11);
}

int resutl = 0;
for (int i=0; i< list.size(); i++) {
resutl
= list.get(i);
}

或者用更高效率的增强的for循环进行遍历(增强for循环和iterator遍历的效果是一样的,也就说增强for循环的内部也就是调用iteratoer实现的)
for (int c2 : list) {

   System.out.println(c2);
}

关于list、Set和map循环读取数据 

1.list读取数据:
Query query = session.createQuery(hql);
   query.setParameter(0, policyNumber);
   List list = query.list();
   if (list != null && list.size() > 0) {
    Iterator it = list.iterator();
    while (it.hasNext()) {
     TPolicyensure tpolicyEnsure = (TPolicyensure) it.next();
     map.put(tpolicyEnsure.getId().getEnsureCode(),DoubleUtil.format(tpolicyEnsure.getInsurance(),2));
     map.put("name_"+tpolicyEnsure.getId().getEnsureCode(),
       tpolicyEnsure.getEnsureShort());
    }
   }

-----------------------------------------------------------------------------------------------------------------------------------

List policylist = service.queryVStatistic(start, parm);

List allPolicyList = service.queryVStatisticAll(parm);

List wsbPolicyList = new ArrayList();

List notWsbPolicyList = new ArrayList();

String wsbProducts=((String)ConstantService.getValue("minoramt","sh_wsb_products",null));

       String[] wps = wsbProducts.split(",");

      for(Iterator<VStatistic> it = allPolicyList.iterator(); it.hasNext(); ){

           VStatistic vs = it.next();

           if(vs.getPolicyNo().startsWith("HAZ") ){

              wsbPolicyList.add(vs);

           } else {

              boolean flag =false;

              for(String wp : wps) {

                  if(wp.equalsIgnoreCase(vs.getProductCode())) {

                     flag = true;

                     wsbPolicyList.add(vs);

                  }

              }

              if(flag ==false){

                  notWsbPolicyList.add(vs);

              }     

           }

==================================================================================

2.Set读取数据:(同List)

===============================================================================

3.Map读取数据:
Map<String, Object> map = new HashMap<String, Object>();
   EpolicyDAO dao = new EpolicyDAO();
   map = dao.getEpolicyEnsure(policy.getPolicyNo());
   Iterator<Entry<String, Object>> iter = map.entrySet().iterator();
   while (iter.hasNext()){
    Entry<String, Object> entry = iter.next();
    String key = entry.getKey();
    String value = null;
    if("01".equals(key)||"02".equals(key)||"03".equals(key)||"04".equals(key)
       ||"05".equals(key)||"06".equals(key)||"07".equals(key)||"08".equals(key)){
     String val = entry.getValue().toString();

     //数据格式化 ###,### (去掉小数点后的零,如12345.00--->12,345)
     value = new DecimalFormat(",###").format(Long.valueOf(val.substring(0, val.length()-3)));
    }
    if ("01".equals(entry.getKey())) {
     VV.setInsurancePremium1(value);
    }
    if ("02".equals(entry.getKey())) {
     VV.setInsurancePremium2(value);
    }
    if ("03".equals(entry.getKey())) {
     VV.setInsurancePremium3(value);
    }
    if ("04".equals(entry.getKey())) {
     VV.setInsurancePremium4(value);
    }
    if ("05".equals(entry.getKey())) {
     VV.setInsurancePremium5(value);
    }
    if ("06".equals(entry.getKey())) {
     VV.setInsurancePremium6(value);
    }
    if ("07".equals(entry.getKey())) {
     VV.setInsurancePremium7(value);
    }
    if ("08".equals(entry.getKey())) {
     VV.setInsurancePremium8(value);
    }
   }

===================================================================

原创粉丝点击