java中遍历rs_Java遍历map和StringBuilder的用法

List> validInfo =perCaseDao.selectValidInfo(perCase);if(validInfo != null && validInfo.size()!=0){

StringBuilder s= new StringBuilder("提交时验证失败!");

StringBuilder s1=newStringBuilder();

StringBuilder s2=newStringBuilder();

StringBuilder s3=newStringBuilder();for (Iterator> it =validInfo.iterator(); it.hasNext();) {

Map 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 newBusinessRuntimeException(s.toString());

}

//获取分类数据并转换为in条件

List> rtd =registTypeDao.selectIntegratedClassDetail();for (Iterator> it =rtd.iterator(); it.hasNext();) {

Map map =it.next();

map.put("registType","'" + ((String) map.get("registType")).replaceAll(",", "','") + "'");

map.put("registTypeDesc","'" + ((String) map.get("registTypeDesc")).replaceAll(",", "','")+ "'");

}//查询当前和累计数据

List> rs1 =integratedDao

.selectCdata(sDate, eDate, rtd,dept);

List> rs2 =integratedDao.selectTdata(eDate, rtd,dept);//合并数据 计算其他项数据

int cl =rtd.size();

Object o;intcAcceptOther;intcRatifyOther;inttAcceptOther;inttRatifyOther;for (int i = 0; i < rs1.size(); i++) {

Map 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);

}returnrs1;

方法一:在for-each循环中使用entry来遍历

Map map = new HashMap();for(Map.Entryentry:map.entrySet()){

System.out.println("key="+entry.getKey()+",value = "+entry.getValue());

方法二:在for-each循环中遍历keys或values

Map map = new HashMap();//遍历map中的键

for(Integer key:map.keySet()){

System.out.println("key="+key);

}//遍历map中的值

for(Integer value:map.values()){

System.out.println("value ="+value);

}

方法三:使用Iterator遍历

Map map = new HashMap();

Iterator> entries =map.entrySet().iterator();while(entries.hasNext()){

Map.Entry entry =entries.next();

System.out.println("key="+entry.getKey()+"value = "+entry.getValue());

}

方法四:不使用泛型

Map map = newHashMap();

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> map = new HashMap>();

map.put("qlrName", new ArrayList());//遍历map

for (Iterator>> it =map.entrySet().iterator(); it

.hasNext();) {

Entry> 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());

}

}

}

HashMap实现原理:https://crossoverjie.top/2018/07/23/java-senior/ConcurrentHashMap/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值