Java遍歷map和StringBuilder的用法


      List<Map<String, Object>> validInfo = perCaseDao.selectValidInfo(perCase);
      if(validInfo != null && validInfo.size()!=0){
        StringBuilder s = new StringBuilder("提交時驗證失敗!");
        StringBuilder s1=new StringBuilder();
        StringBuilder s2=new StringBuilder();
        StringBuilder s3=new StringBuilder();
        for (Iterator<Map<String, Object>> it = validInfo.iterator(); it.hasNext();) {
          Map<String, Object> info = it.next();
          String type = info.get("type").toString();
          Object msg= info.get("msg");
          if("1".equals(type)){
            s1.append(",").append(msg);
          } else if("2".equals(type)) {
            s2.append(",").append(msg);
          } else if("3".equals(type)) {
            s3.append(",").append(msg).append(" 權利單獨共有時不能有多個權利人");
          } else if("4".equals(type)) {
            s3.append(",").append(msg).append(" 按份共有時占有比例之和不等於100%");
          }
        }
        if(s1.length()!=0){
          s.append(s1.substring(1)).append(" 未添加;");
        }
        if(s2.length()!=0){
          s.append(s2.substring(1)).append(" 材料未添加;");
        }
        if(s3.length()!=0){
          s.append(s3.substring(1)).append(";");
        }
        s.append("請檢查確認無誤后再提交。");
        throw new BusinessRuntimeException(s.toString());
      }
    
    //獲取分類數據並轉換為in條件
    List<Map<String, Object>> rtd = registTypeDao.selectIntegratedClassDetail();
    for (Iterator<Map<String, Object>> it = rtd.iterator(); it.hasNext();) {
      Map<String, Object> map = it.next();
      map.put("registType",
          "'" + ((String) map.get("registType")).replaceAll(",", "','") + "'");
      map.put("registTypeDesc",
          "'" + ((String) map.get("registTypeDesc")).replaceAll(",", "','")
              + "'");
    }

    //查詢當前和累計數據
    List<Map<String, Object>> rs1 = integratedDao
        .selectCdata(sDate, eDate, rtd,dept);
    List<Map<String, Object>> rs2 = integratedDao.selectTdata(eDate, rtd,dept);
    
    //合並數據 計算其他項數據
    int cl = rtd.size();
    Object o;
    int cAcceptOther;
    int cRatifyOther;
    int tAcceptOther;
    int tRatifyOther;
    for (int i = 0; i < rs1.size(); i++) {
      Map<String, Object> map = rs1.get(i);
      map.putAll(rs2.get(i));

      o = map.get("cAccept");
      cAcceptOther = o == null ? 0 : ((BigDecimal) o).intValue();
      o = map.get("cRatify");
      cRatifyOther = o == null ? 0 : ((BigDecimal) o).intValue();
      o = map.get("tAccept");
      tAcceptOther = o == null ? 0 : ((BigDecimal) o).intValue();
      o = map.get("tRatify");
      tRatifyOther = o == null ? 0 : ((BigDecimal) o).intValue();

      for (int j = 0; j < cl; j++) {
        o = map.get("cAccept" + j);
        cAcceptOther -= o == null ? 0 : ((BigDecimal) o).intValue();
        o = map.get("cRatify" + j);
        cRatifyOther -= o == null ? 0 : ((BigDecimal) o).intValue();
        o = map.get("tAccept" + j);
        tAcceptOther -= o == null ? 0 : ((BigDecimal) o).intValue();
        o = map.get("tRatify" + j);
        tRatifyOther -= o == null ? 0 : ((BigDecimal) o).intValue();
      }

      map.put("cAcceptOther", cAcceptOther);
      map.put("cRatifyOther", cRatifyOther);
      map.put("tAcceptOther", tAcceptOther);
      map.put("tRatifyOther", tRatifyOther);
    }
    return rs1;
  

 

方法一:在for-each循環中使用entry來遍歷

Map<Integer,Integer> map = new HashMap<Integer,Integer>(); 
for(Map.Entry<Integer,Integer> entry:map.entrySet()){ 
System.out.println("key="+entry.getKey()+",value = "+entry.getValue()); 


方法二:在for-each循環中遍歷keys或values

Map<Integer,Integer> map = new HashMap<Integer,Integer>(); 
//遍歷map中的鍵 
for(Integer key:map.keySet()){ 
System.out.println("key="+key);
} 
//遍歷map中的值 
for(Integer value:map.values()){ 
System.out.println("value ="+value);
}

 

方法三:使用Iterator遍歷

Map<Integer,Integer> map = new HashMap<Integer,Integer>(); 
Iterator<Map.Entry<Integer,Integer>> entries = map.entrySet().iterator(); 
while(entries.hasNext()){
Map.Entry<Integer,Integer> entry = entries.next(); 
System.out.println("key="+entry.getKey()+"value = "+entry.getValue()); 
}

 

方法四:不使用泛型

Map map = new HashMap(); 
Iterator entries = map.entrySet().iterator(); 
while(entries.hasNext()){ 
Map.Entry entry = (Map.Entry)entries.next(); 
Integer key = (Integer)entry.getKey(); 
Integer value = (Integer)entry.getValue(); 
System.out.println("key = "+key+",value = "+value);
}
 

總結
如果只是獲取key,或者value,推薦使用keySet或者values方式

如果同時需要key和value推薦使用entrySet

如果需要在遍歷過程中刪除元素推薦使用Iterator

如果需要在遍歷過程中增加元素,可以新建一個臨時map存放新增的元素,等遍歷完畢,再把臨時map放到原來的map中
 

Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("qlrName", new ArrayList<String>());
//遍歷map
for (Iterator<Entry<String, List<String>>> it = map.entrySet().iterator(); it
        .hasNext();) {
      Entry<String, List<String>> e = it.next();
      mv.addObject(e.getKey(), StringUtils.join(e.getValue(), ','));
    }

//Iterator遍歷list和map

import java.util.*;

 
         

public class TestIterator {

 
         

public static void main(String[] args) {

 
         

List list=new ArrayList();

 
         

Map map=new HashMap();
//初始化list和map的數據
for(int i=0;i<10;i++){

 
         

list.add(new String("list"+i) );

 
         

map.put(i, new String("map"+i));

 
         

}

 
         

Iterator iterList= list.iterator();//List接口實現了Iterable接口
//循環list
while(iterList.hasNext()){

 
         

String strList=(String)iterList.next();

 
         

System.out.println(strList.toString());

 
         

}

 
         

Iterator iterMap=map.entrySet().iterator();
//循環map
while(iterMap.hasNext()){

 
         

Map.Entry strMap=(Map.Entry)iterMap.next();

 
         

System.out.println(strMap.getValue());

 
         

}

 
         

}

 
         

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM